3

私は高低を検索し、さまざまな段階でこれを達成しようとしました。別のテーブルの値に基づいていくつかのテーブルを並べ替えようとしています。v はブール値である ak, 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 )

私が望む結果は、インストールされた that = true が配列を導くように、playerRect とdownloadedImage テーブルをソートすることです..

プレーヤー 1.インストール済み = true 、プレーヤー 2.インストール済み = true 、プレーヤー 3.インストール済み = false

4

1 に答える 1

1

に与えられる比較関数は、厳密な順序関係table.sortを満たさなければなりません。特に、given and if is true thenはfalse を返さなければなりません。abbyInstall(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 に答える