4
我有一個結構如何排序這個lua表?
self.modules = {
["Announcements"] = {
priority = 0,
-- Tons of other attributes
},
["Healthbar"] = {
priority = 40,
-- Tons of other attributes
},
["Powerbar"] = {
priority = 35,
-- Tons of other attributes
},
}
我需要排序先決DESC此表,其他值都無所謂。 例如首先是健康欄,然後是Powerbar,然後是所有其他。
//編輯。
鑰匙必須保留。
//編輯#2
找到了一個解決方案,謝謝大家。
local function pairsByPriority(t)
local registry = {}
for k, v in pairs(t) do
tinsert(registry, {k, v.priority})
end
tsort(registry, function(a, b) return a[2] > b[2] end)
local i = 0
local iter = function()
i = i + 1
if (registry[i] ~= nil) then
return registry[i][1], t[registry[i][1]]
end
return nil
end
return iter
end
你是什麼意思排序?帶有字符串鍵的表格未排序。要對其進行排序,您需要更改數據結構。你的預期結果是什麼? – ahilsend
[在Lua中排序表\ [\]]的可能重複(http://stackoverflow.com/questions/15706270/sort-a-table-in-lua) – ahilsend