私は、変数T
に保存されている次のLuaテーブルを持っている:ルアの内部テーブルを "スコア"と "インデックス"でソートする方法は?
{
["mn"] = { ["index"] = 7, ["key"] = "mn", ["score"] = 0 },
["kl"] = { ["index"] = 6, ["key"] = "kl", ["score"] = .4 },
["ef"] = { ["index"] = 3, ["key"] = "ef", ["score"] = .3 },
["ab"] = { ["index"] = 1, ["key"] = "ab", ["score"] = 0 },
["cd"] = { ["index"] = 2, ["key"] = "cd", ["score"] = .1 },
["gh"] = { ["index"] = 4, ["key"] = "gh", ["score"] = 0 },
["ij"] = { ["index"] = 5, ["key"] = "ij", ["score"] = .2 }
}
私は次のようにT
テーブルの内部テーブルのすべてをソートしたい:高いscore
と
1.テーブルを置くされていますトップ。
2.等価score
の表は、index
でソートされています。
ので、ソートした後、次の順次の表は、出力に生成する必要があります。
{
[1] = { ["index"] = 6, ["key"] = "kl", ["score"] = .4 }, -- highest "score"
[2] = { ["index"] = 3, ["key"] = "ef", ["score"] = .3 },
[3] = { ["index"] = 5, ["key"] = "ij", ["score"] = .2 },
[4] = { ["index"] = 2, ["key"] = "cd", ["score"] = .1 },
[5] = { ["index"] = 1, ["key"] = "ab", ["score"] = 0 }, -- lowest "score", lowest "index"
[6] = { ["index"] = 4, ["key"] = "gh", ["score"] = 0 }, -- when scores are the same, sort by their "index" instead
[7] = { ["index"] = 7, ["key"] = "mn", ["score"] = 0 } -- lowest "score", highest "index"
}
このLuaのテーブルの並べ替えを達成するためにどのように?
https://devdocs.io/lua~5.3/index#pdf-table.sort – hjpotter92
私は 'table.sort' Luaの機能を使用する必要があることを理解しかし、私はこの場合にどのように使用するのか分かりません。 – Pojat