魔兽世界Lua - 排序表

时间:2016-06-24 05:12:16

标签: lua add-on world-of-warcraft

我正在尝试为addon / weakaura排序表,但我不知道该怎么做(按attribut值排序)。

示例:

        player = {
            value = 34
            class = Warrior,
            id    = 1
        },  
        {
            value = 1,
            class = mage,
            id    = 2
        },
        {
            value = 3443,
            class = Paladin,,
            class = 3
        }    

我想:

          player = {
            value = 1,
            class = mage,
            id    = 2
        },
        {
            value = 34
            class = Warrior,
            id    = 1
        },  
        {
            value = 3443,
            class = Paladin,
            class = 3
        }   

有人知道如何做到这一点?

1 个答案:

答案 0 :(得分:2)

假设您修复了表声明,可以使用自定义函数对表进行排序:

local player = {
  { value = 34, class = "Warrior", id = 1 },
  { value = 1, class = "mage",  id = 2 },
  { value = 3443, class = "Paladin", id = 3 },
}

table.sort(player, function(a,b) return a.value < b.value end)

这将按player字段的值对value表的元素进行排序。如果需要,您可以提出更精细的分类条件。

有关table.sort

的更多信息,请参阅http://www.lua.org/manual/5.3/manual.html#pdf-table.sort