2017-09-08 12 views
2

私は、変数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のテーブルの並べ替えを達成するためにどのように?

+0

https://devdocs.io/lua~5.3/index#pdf-table.sort – hjpotter92

+0

私は 'table.sort' Luaの機能を使用する必要があることを理解しかし、私はこの場合にどのように使用するのか分かりません。 – Pojat

答えて

1

最初に持っているハッシュをテーブルに変換してから、score(降順)、次にindex(昇順)の並べ替えを持つカスタムソート関数を使用して、そのテーブルの要素を並べ替える必要があります。同じスコア。

このような何かが動作するはず

local hash = { 
    ["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 } 
} 
local tbl = {} 
for _,v in pairs(hash) do 
    table.insert(tbl, v) 
end 
table.sort(tbl, function(a,b) 
    return a.score > b.score or a.score == b.score and a.index < b.index 
    end) 
+0

助けてくれてありがとう! :D – Pojat

1

表には、配列と辞書の2つのデータ構造が含まれています。各要素は、数値インデックスとインデックスに関連付けられた配列を、ソーティング

ソート手段が連続していることである:1,2,3 ...

あなた初期テーブルは、実際には辞書である - 各エントリは任意有しますそれに関連付けられたキー(あなたの場合は文字列です)。

このように、実際にソート作業ではなく、最終的に別の種類の表が必要です。

table.sortは、1から始まり最初のnilエントリで終わるインデックスを持つ要素上にあるluaテーブルの配列部分に作用します。

a={s=3,['r']=3, 5,3,2, nil,21} 
       |these| 
       |ones | 

だから、あなたが最初の配列を作成し、1という並べ替え:

local sorted={} 
for k,v in pairs(T) do 
    table.insert(sorted,v) 
end 
table.sort(sorted,function(a,b) 
    --your code here 
    --function should return true if element `a` from the array `sorted` 
    --must be higher (to the left of) `b` 
end) 

また、あなたは両方の辞書や配列の一部で同じテーブル内のエントリを格納することができ、table.sort関数は無視するだろう辞書。しかし、pairsを使ってテーブルをループして、同時に新しい要素を追加することは賢明ではありません。したがって、慣用的な方法は依然として中間コピーを伴う。

+0

ありがとうございます。 – Pojat

関連する問題