您的位置:首页 > 编程语言 > Lua

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: