3

我一直在寻找高低,并试图在不同的阶段实现这一目标。我正在尝试对基于另一个表值的几个表进行排序。我试图通过播种 ak, 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 )

我希望对 playerRect 和下载的图像表进行排序的结果,以便安装的 that = true 将引导数组..

播放器 1.installed = true ,播放器 2.installed = true ,播放器 3.installed = false

4

1 回答 1

1

给定的比较函数table.sort必须满足严格的排序关系。特别是,给定ab如果byInstall(a, b)为真则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将如下所示:

{
  {
    "steve",
    installed = true
  },
  {
    "dave",
    installed = true
  },
  {
    "carol",
    installed = true
  },
  {
    "bob",
    installed = true
  },
  {
    "matthew",
    installed = false
  },
  {
    "alice",
    installed = false
  }
}
于 2013-11-14T06:22:37.563 回答