• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

luatable.sort的bug

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

总结:不能用<=,不然会报错。当<与==拆分的时候,==(如果是最后一个if语句)必须要返回false。

 

> tbl = {{4,1},{4,2},{3,3},{5,3},{5,1},{3,2},{5,4},{5,5},{5,1},{5,3},{3,3},{4,2},{3,2}}
> function comp(a,b)
>> if a[1] <= b[1] then
>> return true
>> else
>> return false
>> end
>> end
> table.sort(tbl, comp)
stdin:1: invalid order function for sorting
stack traceback:
        [C]: in function 'sort'
        stdin:1: in main chunk
        [C]: in ?
> table.sort(tbl,comp)
stdin:1: invalid order function for sorting
stack traceback:
        [C]: in function 'sort'
        stdin:1: in main chunk
        [C]: in ?
> function comp(a,b)  
if a[1] < b[1] then
return true
else
return false
end
end
> table.sort(tbl,comp)
>  for i, _t in pairs(tbl) do                                                          
>> print(i, _t[1], _t[2])
>> end
1       3       3
2       3       2
3       3       3
4       3       2
5       4       1
6       4       2
7       4       2
8       5       3
9       5       4
10      5       1
11      5       3
12      5       1
13      5       5
> tbl = {{4,1},{4,2},{3,3},{5,3},{5,1},{3,2},{5,4},{5,5},{5,1},{5,3},{3,3},{4,2},{3,2}}
> function comp(a,b) 
if a[1] < b[1] then
return true
elseif a[1] == b[1] then
return false
end
end
> function comp(a,b)
if a[1] < b[1] then
return true
elseif a[1] == b[1] then
return false
else return false end
end
>  for i, _t in pairs(tbl) do                                                          
print(i, _t[1], _t[2])
end
1       4       1
2       4       2
3       3       3
4       5       3
5       5       1
6       3       2
7       5       4
8       5       5
9       5       1
10      5       3
11      3       3
12      4       2
13      3       2
> function comp(a,b) 
if a[1] < b[1] then
return true
else
return false
end
end
> table.sort(tbl,comp)
>  for i, _t in pairs(tbl) do                                                          
print(i, _t[1], _t[2])
end
1       3       2
2       3       3
3       3       3
4       3       2
5       4       2
6       4       1
7       4       2
8       5       3
9       5       1
10      5       3
11      5       5
12      5       1
13      5       4
> tbl = {{4,1},{4,2},{3,3},{5,3},{5,1},{3,2},{5,4},{5,5},{5,1},{5,3},{3,3},{4,2},{3,2}}
> function comp1(a, b)                                                                
>> if a[1] < b[1] then
>> return true
>> elseif a[1] == b[1] then
>> if a[2] < b[2] then
>> return true
>> else
>> return false
>> end
>> else
>> return false
>> end
>> end
> function comp1(a, b)                                                                
if a[1] < b[1] then
return true
elseif a[1] == b[1] then
if a[2] <= b[2] then
return true
else
return false
end
else
return false
end
end
> table.sort(tbl,comp)
> tbl = {{4,1},{4,2},{3,3},{5,3},{5,1},{3,2},{5,4},{5,5},{5,1},{5,3},{3,3},{4,2},{3,2}}
> function comp1(a, b)                                                                
if a[1] < b[1] then
return true
elseif a[1] == b[1] then
if a[2] <= b[2] then
return true
else
return false
end
else
return false
end
end
> table.sort(tbl,comp1)
stdin:1: invalid order function for sorting
stack traceback:
        [C]: in function 'sort'
        stdin:1: in main chunk
        [C]: in ?
> tbl = {{4,1},{4,2},{3,3},{5,3},{5,1},{3,2},{5,4},{5,5},{5,1},{5,3},{3,3},{4,2},{3,2}}
> function comp1(a, b)                                                                
if a[1] < b[1] then
return true
elseif a[1] == b[1] then
if a[2] < b[2] then
return true elseif a[2] == a[3] then return false
else
return false
end
else
return false
end
end
> table.sort(tbl,comp1)
>  for i, _t in pairs(tbl) do                                                          
print(i, _t[1], _t[2])
end
1       3       2
2       3       2
3       3       3
4       3       3
5       4       1
6       4       2
7       4       2
8       5       1
9       5       1
10      5       3
11      5       3
12      5       4
13      5       5
> tbl = {{4,1},{4,2},{3,3},{5,3},{5,1},{3,2},{5,4},{5,5},{5,1},{5,3},{3,3},{4,2},{3,2}}
> function comp1(a, b)                                                                
if a[1] < b[1] then
return true
elseif a[1] == b[1] then
if a[2] < b[2] then
return true elseif a[2] == a[3] then return true
else
return false
end
else
return false
end
end
> table.sort(tbl,comp1)
>  for i, _t in pairs(tbl) do                                                          
print(i, _t[1], _t[2])
end
1       3       2
2       3       2
3       3       3
4       3       3
5       4       1
6       4       2
7       4       2
8       5       1
9       5       1
10      5       3
11      5       3
12      5       4
13      5       5
>  function comp(a, b)                       
>> if a[1] < b[1] then
>> return true
>> elseif a[1] == b[1] then
>> if a[2] < b[2] then
>> return true
>> elseif a[2] == b[2] then
>> return true
>> else
>> return false
>> end
>> else
>> return false
>> end
>> end
> tbl = {{4,1},{4,2},{3,3},{5,3},{5,1},{3,2}}
> table.sort(tbl,comp)
>  for i, _t in pairs(tbl) do                                                          
print(i, _t[1], _t[2])
end
1       3       2
2       3       3
3       4       1
4       4       2
5       5       3
6       5       1
>  function comp(a, b)                       
if a[1] < b[1] then
return true
elseif a[1] == b[1] then
if a[2] < b[2] then
return true
elseif a[2] == b[2] then
return false
else
return false
end
else
return false
end
end
> tbl = {{4,1},{4,2},{3,3},{5,3},{5,1},{3,2}}
> table.sort(tbl,comp)
>  for i, _t in pairs(tbl) do                                                          
print(i, _t[1], _t[2])
end
1       3       2
2       3       3
3       4       1
4       4       2
5       5       1
6       5       3


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Lua IDE 比较和选择发布时间:2022-07-22
下一篇:
lua 源码分析之线程对象lua_State发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap