lua table.sort的bug
2013-04-08 18:13
453 查看
总结:不能用<=,不然会报错。当<与==拆分的时候,==(如果是最后一个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
> 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 排序算法和性能分析[1]:table.sort
- lua的table.sort 中出现 invalid order function for sorting
- Lua table sort
- Lua 中 table 库函数 insert、remove、sort
- lua中table.sort的回调函数
- Lua 学习笔记 (6)table.sort
- Lua 中 table.sort 的比较函数
- lua table note,table.sort ,table.concat
- Lua中table.sort() 报错 attempt to compare number with nil和invalid order function for sorting
- lua中table.sort用法问题
- lua table sort
- lua的table.sort 中出现 invalid order function for sorting
- lua之table.sort使用
- lua的table.sort
- Lua打印Table内容(日期)
- (原创) cocos2d-x 3.0+ lua 学习和工作(4) : 公共函数(7): 其他table方法
- Jquery.Sorttable 桌面拖拽自定义
- lua的sort排序
- Lua笔记3-table
- salesforce table column sort