Lua排序表

时间:2013-07-25 11:16:26

标签: sorting lua lua-table

我对Lua一无所知,但我能够修改我想要的脚本。我虽然在排序桌子时遇到了麻烦。

我找到了table utils(将表转换为字符串),这是我的表:

{{line="(Golden Aura) Challenging An owl would be either very brave or very stupid.",range="(+16 to +21)",message="(Golden Aura) Challenging An owl would be either very brave or very stupid.",colour="crimson",srt=9,keyword="owl",name="An owl"},
{line="(Golden Aura) A busy squirrel chuckles at the thought of you fighting him.",range="(+3 to +8)",message="(Golden Aura) A busy squirrel chuckles at the thought of you fighting him.",colour="gold",srt=7,keyword="squirrel",name="(Golden Aura) A busy squirrel"},
{line="(Red Aura) A parakeet should be a fair fight!",range="(-2 to +2)",message="(Red Aura) A parakeet should be a fair fight!",colour="springgreen",srt=5,keyword="parakeet",name="(Red Aura) A parakeet"},
{line="(Golden Aura) Challenging A cat would be either very brave or very stupid.",range="(+16 to +21)",message="(Golden Aura) Challenging A cat would be either very brave or very stupid.",colour="crimson",srt=9,keyword="cat",name="A cat"}}

我能够添加srt键,我希望按此排序表。有人可以告诉我该怎么做,拜托?

1 个答案:

答案 0 :(得分:4)

table.sort( table:t [, function( left, right ):sorting function ] )

因此,由于您希望按v.srt排序,您可以执行以下操作:

table.sort( t, function( a, b ) return a.srt < b.srt end )

for k, v in pairs( t ) do
  print( v.srt, v.name )
end

哪个应按升序排序,然后显示它们。