lua table.sort的bug

总结:不能用<=,不然会报错。当<与==拆分的时候,==(如果是最后一个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

分类:  lua
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值