我进行了高低搜索,并尝试在不同阶段实现这一目标。 我正在尝试根据另一个表值对几个表进行排序。我尝试通过为 k、v 播种来对这些表进行排序,其中 v 是一个 bool 值。
这是我到目前为止所得到的。提前感谢您的帮助。
function byInstallsField(x,y)
-- cant seem to make sens out of the sorting
if x.installed then return true
elseif y.installed == false then return false
end
end
-- var is the index passed into the function
-- pRect:new is a custom lib for rect' drawing
playerRect[ var ] = pRect:new(_W + dimesion_for_rect * var, 0,
dimesion_for_rect,
dimesion_for_rect,
3 ) -- _x, _y , _hieght, _width, round
playerRect.installed = inst;
table.sort( playerRect, byInstallsField )
downloadedImage[ var ] = fb_friends:new(var, imageOnFile,
playerRect[ var ].x,
playerRect[ var ].y,
0.25, 0.25, 0, dimesion_for_rect - 5)
downloadedImage[ var ].id = var
downloadedImage.installed = inst
table.sort( downloadedImage, byInstallsField )
我希望的结果是对 playerRect 和下载的图像表进行排序,以便安装的 that = true 将引导数组..
player 1.installed = true , player 2.installed = true , player 3.installed = false
最佳答案
table.sort
的比较函数必须满足严格排序 关系。特别是,给定 a
和 b
如果 byInstall(a, b)
为真,则 byInstall(b, a)
必须返回 false。
这是一个简单的例子:
local player = {
{"alice", installed = false},
{"bob", installed = true},
{"carol", installed = true},
{"dave", installed = true},
{"matthew", installed = false},
{"steve", installed = true},
}
function byInstall(first, second)
return first.installed and not second.installed
end
table.sort(player, byInstall)
排序后,installed = true
的子表将被分组在一起。表 player
将如下所示:
{
{
"steve",
installed = true
},
{
"dave",
installed = true
},
{
"carol",
installed = true
},
{
"bob",
installed = true
},
{
"matthew",
installed = false
},
{
"alice",
installed = false
}
}
关于使用 bool 值表对 lua 表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19967696/