【问题标题】:Advanced table sorting in lualua中的高级表格排序
【发布时间】:2014-08-11 04:52:18
【问题描述】:

我正在尝试对高级表格进行排序,但没有成功。

这是我的表结构的样子:

{
    ["12345"] = {12345, "Something", {"Stuff"}},
    ["523544"] = {523544, "Something", {"Stuff"}},
    ["6744"] = {6744, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["724572"] = {724572, "Something", {"Stuff"}},
    ["54"] = {54, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["12345"] = {12345, "Something", {"Stuff"}},
    ["44"] = {44, "Something", {"Stuff"}},
}

我想像这样从大到小对它进行排序:

{
    ["724572"] = {724572, "Something", {"Stuff"}},
    ["523544"] = {523544, "Something", {"Stuff"}},
    ["12345"] = {12345, "Something", {"Stuff"}},
    ["12345"] = {12345, "Something", {"Stuff"}},
    ["6744"] = {6744, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["54"] = {54, "Something", {"Stuff"}},
    ["44"] = {44, "Something", {"Stuff"}},
}

我在这里遇到了一些问题。

  1. 不能保存2个等值的数字
  2. 我似乎无法正确地从大到小排序

至于为什么索引是字符串,如果我做table[623258195] = "Example",表会创建623258195个索引,导致我的程序崩溃。

至于为什么值是表,它存储了其他重要信息,即表中的第2个和第3个值是什么,第一个是索引的数字形式。

我希望我说的很清楚,如果这被认为是重复的问题,我很抱歉,我在最后一小时的搜索中没有找到任何对我有帮助的东西。

【问题讨论】:

  • 您不能在不使用整数键的情况下对表进行排序。您可以使用整数键创建第二个有序表,其值等于未排序表中的相应键。稍后我会发布一个示例。
  • 另外 -- 使用 table[largenum] 将不会(不应该?)创建高达 largenum 的索引。
  • 我再次测试了table[largenum],看起来你是对的,但我记得不久前遇到过这个问题。
  • 但是,我仍然不能使用索引来保存值,因为我需要能够保存多个相同的数字。在此期间,我将尝试您所说的内容。
  • "如果我执行 table[623258195] = "Example",该表将创建 623258195 个索引" Lua 的哪个实现会这样?标准的没有。

标签: sorting lua lua-table


【解决方案1】:
  1. 您需要修改数据结构,以支持具有相同 id/key 的多个值:

    {
        [12345] = {
            {12345, "foo", {"bar"}}, -- You'll probably want to sort these somehow.
            {12345, "baz", {"qux"}}
        },
        [123] = {
            {123, "foo", {"bar"}}
        }
    }
    
  2. 您可以使用table.sort(tbl, f),以及一个索引表:

    local unsorted = {} -- Data, in your format, inside this table.
    local index = {} -- Table which will contain sorted keys (then you loop over this, get unsorted[k])
    for k in pairs(unsorted) do
        index[#index+1] = k -- Populate the keys to sort.
    end
    table.sort(index, function(a, b) 
     return b < a -- Order highest to lowest, instead of lowest - highest (default)
    end)
    

这是完整的代码示例和结果。 http://ideone.com/thE1zP

【讨论】:

  • 如果您需要更多解释,请告诉我。对于多个键,我忘了提,你只想分支:tbl[1234] = {[1] = {1234, "data"}, [2] = {1234, "otherdata"}}}
【解决方案2】:

您不能对哈希中的键进行“排序”,因为它们不像表中键的整数序列那样具有“顺序”。

您可以更改数据结构以将散列转换为表,但更简单的方法可能是使用仅包含散列键的单独表并对它们的值进行排序;当您需要以特定顺序从哈希中获取元素时,您只需遍历该表中的(排序的)元素,然后从哈希中检索元素。

在任何一种情况下,您都无法像尝试那样为同一个键存储多个值:

{
...
    ["146"] = {146, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
...
}

您需要将它们存储在一个表中(并使用"146" 键引用该表)或重新考虑为什么需要使用相同键值的不同元素键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 2013-11-28
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2011-10-07
    相关资源
    最近更新 更多