• 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

  • 相关阅读:
    Predicting survival from colorectal cancer histology slides using deep learning: A retrospective multicenter study翻译
    pytorch加载训练好的模型用来测试或者处理-finetuning-微调-转移学习
    vgg16 代码 pytorch(nice~)
    git使用命令
    打包时ElementUI使vendor.js文件体量过大优化方法
    webpack打包优化
    hibernate和 mybatis的区别
    java 关于JDBC和DAO模式使用
    JSONP获取JSON数据
    SpringMVC通过CROS协议配置跨域访问
  • 原文地址:https://www.cnblogs.com/ghost240/p/3008587.html
Copyright © 2020-2023  润新知