使用布尔值表对lua表进行排序

时间:2013-11-14 01:15:59

标签: sorting lua corona lua-table

我搜索过高低,并尝试在不同阶段完成此任务。 我正在尝试根据另一个表值对几个表进行排序。我试图通过播种k,v来对这些表进行排序,其中v是一个布尔值。

这是我到目前为止所做的事情。并提前感谢您的帮助。

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 )

我希望玩家拥有玩家的结果&要对已下载的映像表进行排序,以便安装的那个= true将引导数组..

  

player 1.installed = true,player 2.installed = true,player 3.installed = false

1 个答案:

答案 0 :(得分:0)

赋予table.sort的比较函数必须满足严格排序关系。特别是,如果ab,则byInstall(a, b) <{1}} 必须 返回false。

这是一个简单的例子:

byInstall(b, a)

排序后,带有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