排序

来源:互联网 发布:搜读书笔记软件 编辑:程序博客网 时间:2024/05/01 18:07
require "print_r"
----------------------------------------------
names = {"Peter", "Paul", "Mary"}

grades = {Mary = 10, Paul = 7, Peter = 8}


table.sort(names, function (n1, n2)

    return grades[n1] > grades[n2]    -- compare the grades

end)


print_r(names)


写成函数同样效果

function sortbygrade (names, grades)
    table.sort(names, function (n1, n2)
       return grades[n1] > grades[n2]    -- compare the grades
    end)
end