简体   繁体   English

Lua表排序不起作用

[英]Lua table sort does not work

I have the below program code which tries to sort a given list. 我有下面的程序代码,试图对给定的列表进行排序。 I have tried various options and it still doesn`t work. 我尝试了各种选项,但仍然无法正常工作。

local List = {}    
List[143] = "143"    
List[145] = "145"    
List[120] = "120"       
List[178] = "178"   
table.sort(List, compare)

compare function is defined as 比较功能定义为

function compare(a, b)    
    if tonumber(a) < tonumber(b) then    
        return true    
    end
end

Above table.sort does not work to any order. 以上table.sort不适用于任何订单。 I just want to sort it to increasing numerical order. 我只想将它排序为增加数字顺序。 If you have any ideas about this please help me. 如果您对此有任何想法,请帮助我。 Thanks in advance 提前致谢

table.sort (and much of the rest of the table.* functions) is defined only for operations on array-like tables. table.sort (以及table.*的其余部分table.*函数)仅针对类似数组的表上的操作进行定义。 That means tables with contiguous integer keys from 1..n . 这意味着具有1..n连续整数键的表。 Your table doesn't meet those criteria. 您的表格不符合这些标准。

The keys of that table do not satisfy requirements of that function. 该表的键不满足该功能的要求。 The keys must start at 1 and sequentially increase to N, as per Lua ref manual ("Sorts table elements in a given order, in-place, from table[1] to table[n] "). 密钥必须从1开始并按顺序增加到N,按照Lua ref手册 (“按给定顺序对表格元素进行排序,就地,从table[1]table[n] ”)。 Try 尝试

local List = {}
List[1] = "143"    
List[2] = "145"    
List[3] = "120"       
List[4] = "178"   
table.sort(List, compare)

or even better 甚至更好

local List = {"143", "145", "120", "178"}
table.sort(List, compare)

Key-value pairs in a table don't have a stable order. 表中的键值对没有稳定的顺序。 For each run through with the pairs or next functions, you may see a different sequence of key-value pairs. 对于每次运行pairsnext函数,您可能会看到不同的键值对序列。 (Presumably the sequence might change as a result of adding or removing from the table.) (据推测,由于在表格中添加或删除,序列可能会发生变化。)

Except for setting or getting at specific keys, other table operations use the positive integer keys. 除了设置或获取特定键之外,其他表操作使用正整数键。 maxn gets the maximum key n that does not have a nil value. maxn获取没有nil值的最大键n。 The others assume the keys are contiguous. 其他人认为密钥是连续的。 For such tables, the positive integer keys can be considered to have a definite range and are ordered so sorting would make sense. 对于这样的表,正整数键可以被认为具有确定的范围并且是有序的,因此排序是有意义的。

The positive integer keys in List are not contiguous. List中的正整数键不是连续的。 So, sort 's behavior is not useful (and, in general, non-deterministic). 因此, sort的行为没有用(并且通常是非确定性的)。

Perhaps, you wanted something like this operation over all positive integer keys: 也许,你需要像所有正整数键这样的操作:

local values = {}
-- extract values from positive integer keys
for key, value in pairs(List)
    if (type(key) = "number") and key > 0 then
        table.insert(values, value)
        List[key] = nil    
    end
end
-- sort and restore them to the table
table.sort(values, function(a,b) return tonumber(a)<tonumber(b) end)
for key, value in values
    List[key] = value
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM