欢迎使用CSDN-markdown编辑器

来源:互联网 发布:php跳转url代码 编辑:程序博客网 时间:2024/06/10 12:43

lua table索引无序 实现多重排序

--lua table索引无序 实现多重排序 直接看代码 费话少说--目标 优先级: chinese > english > historytable_score = {    [1] = {        chinese = 80,        english = 76,        history = 100,    },    [22] = {        chinese = 80,        english = 76,        history = 99,    },    [33] = {       chinese = 79,        english = 60,        history = 100,    },    [4] = {        chinese = 70,        english = 60,        history = 98,    },    [53] = {        chinese = 70,        english = 61,        history = 75,    },}--相当于把table_score放到一个新表中local i = 1local table_tmp = {}for k,v in pairs(table_score)do    table_tmp[i] = {chinese = v.chinese, english = v.english, history = v.history}    i = i + 1end--升序local function sortFunc_1(a, b)    if a.chinese == b.chinese then        if a.english == b.english then            return a.history < b.history        else            return a.english < b.english        end    else        return a.chinese < b.chinese    endend--降序local function sortFunc_2(a, b)    if a.chinese == b.chinese then        if a.english == b.english then            return a.history > b.history        else            return a.english > b.english        end    else        return a.chinese > b.chinese    endend--排序table.sort(table_tmp, sortFunc_2)--测试for k,v in pairs(table_tmp) do    print(k,v.chinese,v.english,v.history)end

排序结果