sorting lua tables with a table of boolean values
我已经从高处和低处搜索过,并试图在不同的阶段实现这一目标。
我正在尝试对基于另一个表值的几个表进行排序。我尝试通过播种 k, v 来对这些表进行排序,其中 v 是布尔值。
这就是我到目前为止所得到的.. 提前感谢您的帮助。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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 的结果
给
这是一个简单的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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) |
排序后,带有
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | { { "steve", installed = true }, { "dave", installed = true }, { "carol", installed = true }, { "bob", installed = true }, { "matthew", installed = false }, { "alice", installed = false } } |