简体   繁体   English

如何在lua中对表进行排序?

[英]how to sort a table in lua?

I have a lua table that contains 2 key pieces of data. 我有一个lua表,其中包含2个关键数据。 I would like to sort the table in ascending order by the "num1" column, or if thats not possible, they by the key value in ascending order 我想按“ num1”列按升序对表进行排序,或者如果不可能,则按键值按升序对表进行排序

Here's what I have so far: 这是我到目前为止的内容:

local widgets = {}
widgets[1568] = {}
widgets[1568]["num1"] = 99999
widgets[1568]["val2"] = "NA"
widgets[246] = {}
widgets[246]["num1"] = 90885
widgets[246]["val2"] = "NA"
widgets[250] = {}
widgets[250]["num1"] = 95689
widgets[250]["val2"] = "NA"
widgets[251] = {}
widgets[251]["num1"] = 95326
widgets[251]["val2"] = "NA"
widgets[252] = {}
widgets[252]["num1"] = 95301
widgets[252]["val2"] = "NA"
widgets[256] = {}
widgets[256]["num1"] = 95303
widgets[256]["val2"] = "NA"

-- ATTEMPT TO SORT
--table.sort(widgets, function(a,b) return tonumber(a.num1.value) < tonumber(b.num1.value) end)
--table.sort(widgets, function(a,b) return tonumber(a.num1) < tonumber(b.num1) end)

--TRY SORTING BY ID:
table.sort(widgets, function(a,b) return tonumber(a) < tonumber(b) end)

for i, v in pairs(widgets) do
    print(v.num1)
end

Any suggestions would be appreciated. 任何建议,将不胜感激。 Right now, I'm reviewing Sort a Table[] in Lua to try to understand the "spairs" function. 现在,我正在审查Lua中的Sort a Table [],以尝试了解“配对”功能。 But that example is slightly different because I have a table within a table... 但是该示例略有不同,因为我在表中有一个表...

Thanks. 谢谢。

SOLUTION

In line with the answer below, I created a new table and added the records from the old table, one by one, using table insert like so: 按照下面的答案,我创建了一个新表,并使用表插入方式逐一添加了旧表中的记录,如下所示:

local new_widgets = {}
for i, v in pairs(widgets) do
    table.insert(new_widgets, id=v.id, num1= v.num1, num2 = v.num2)
end

then I sorted new_wigets. 然后我对new_wigets进行了排序。

Lua tables are hashtables. Lua表是哈希表。 Their entries have no specific order. 他们的条目没有特定的顺序。

You fake it by using consecutive numerical indices then iterating by incrementing a number (note: internally Lua actually will implement this as an array, but that's an implementation detail; conceptually, table entries have no specific order). 您可以通过使用连续的数字索引来伪造它,然后通过增加一个数字来进行迭代(注意:Lua实际上将内部实现为数组,但这是实现的细节;概念上,表条目没有特定的顺序)。

t[2] = "two"
t[3] = "three"
t[1] = "one"

for i=1,#t do print(t[i]) end

ipairs creates an iterator that does the same thing as this for loop. ipairs创建一个迭代器,该迭代器与此for循环执行相同的操作。

So, if you want your data sorted, you need to put it in a table with consecutive numeric indices. 因此,如果要对数据进行排序,则需要将其放入具有连续数字索引的表中。

In your case, there's a lot of different ways you can do it. 在您的情况下,有很多不同的方法可以执行此操作。 Here's one way to skin that cat: 这是给那只猫剥皮的一种方法:

Instead of this: 代替这个:

local widgets = {
    [246] = { num1 = 90885, val2 = "NA" }
    [250] = { num1 = 95689, val2 = "NA" }
    [251] = { num1 = 95326, val2 = "NA" }
    [252] = { num1 = 95301, val2 = "NA" }
    [256] = { num1 = 95303, val2 = "NA" }
}

You want this: 你要这个:

local widgets = {
    { id = 246, num1 = 90885, val2 = "NA" },
    { id = 250, num1 = 95689, val2 = "NA" },
    { id = 251, num1 = 95326, val2 = "NA" },
    { id = 252, num1 = 95301, val2 = "NA" },
    { id = 256, num1 = 95303, val2 = "NA" },
}

-- sort ascending by num1
table.sort(widgets, function(a,b) return a.num1 < b.num1 end)

for i, widget in ipairs(widgets) do
    print(widget.num1)
end

If you need the ability to then lookup a widget quickly by id, you can create a lookup table for that: 如果您需要通过ID快速查找小部件的功能,则可以为此创建一个查找表:

local widgetById = {}
for i,widget in pairs(widgets) do
    widgetById[widget.id] = widget
end

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

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