如何重新了解Lua中的table.sort函数?

时间:2019-11-06 10:45:01

标签: lua

> network = {
>>        {name = "grauna",  IP = "210.26.30.34"},
>>        {name = "arraial", IP = "210.26.30.23"},
>>        {name = "lua",     IP = "210.26.23.12"},
>>        {name = "derain",  IP = "210.26.23.20"},
>>      }
> table.sort(network, function (a,b)
>>       return (a.name > b.name)
>>     end)
> 

然后,我尝试打印出表格元素的排序版本,如下所示。

> for i,data in ipairs(network) do
>>   print(index)
>>   for key, value in pairs(data) do
>>      print('\t', key, value)
>>   end
>> end
nil
        IP  210.26.23.12
        name    lua
nil
        IP  210.26.30.34
        name    grauna
nil
        IP  210.26.23.20
        name    derain
nil
        IP  210.26.30.23
        name    arraial
> 

任何评论都将不胜感激。

0 个答案:

没有答案