local test0 ={1,9,2,8,3,7,4,6}
table.sort(test0) --从小到大排序
for i,v in pairs(test0) do
io.write(v.." ")
end
print("");
table.sort(test0,function(a,b) return a>b end) --从大到小排序
for i,v in pairs(test0) do
io.write(v.." ")
end
print(" ")
local test2 ={
{id=1, name="deng"},
{id=9, name="luo"},
{id=2, name="yang"},
{id=8, name="ma"},
{id=5, name="wu"},
}
table.sort(test2,function(a,b) return a.idb.id end )
for i in pairs(test2) do
print(test2[i].id,test2[i].name)
end
local test1 ={a=1,f=9,d=2,c=8,b=5}
local key_test ={}
for i in pairs(test1) do
table.insert(key_test,i) --提取test1中的键值插入到key_test表中
end
table.sort(key_test)
for i,v in pairs(key_test) do
print(v,test1[v])
end