在用table.sort 排序的时候注意,如果使用多个条件排序,应在一个排序函数里按照条件优先级进行比较排序。

例如

local t = {
{time = , i = },
{time = , i = },
{time = , i = },
{time = , i = },
{time = , i = },
{time = , i = },
}

现要求按 i 排序,i 相同时按 time 排序,

假如用两次排序

1、先用time排序

table.sort(t, function(t1, t2) return t1.time > t2.time end)
print("t = {")
for i, v in ipairs(t) do
print(string.format("\t[%d] = {time = %d, i = %d}", i, v.time, v.i))
end
print("}")

排序后结果:

t = {
[] = {time = , i = }
[] = {time = , i = }
[] = {time = , i = }
[] = {time = , i = }
[] = {time = , i = }
[] = {time = , i = }
}

此时再按 i 排序

table.sort(t, function(t1, t2) return t1.i > t2.i end)

print("t = {")
for i, v in ipairs(t) do
print(string.format("\t[%d] = {time = %d, i = %d}", i, v.time, v.i))
end
print("}")

期望 i 相等时(i = 2)能得到和按 time 排序后的一样的相对位置,即:

t = {
[] = {time = , i = }
[] = {time = , i = }
[] = {time = , i = }
[] = {time = , i = }
[] = {time = , i = }
[] = {time = , i = }
}

然而实际结果却是:

t = {
[] = {time = , i = }
[] = {time = , i = }
[] = {time = , i = }
[] = {time = , i = }
[] = {time = , i = }
[] = {time = , i = }
}

这应该是table.sort的内部排序算法造成的。

所以,在多个条件下排序需要一个排序函数,只调用table.sort()一次。而且多次排序也影响性能。

table.sort(t, function(t1, t2)
if t1.i == t2.i then
return t1.time > t2.time
else
return t1.i > t2.i
end
end)

Lua的 table.sort排序的更多相关文章

  1. Lua table.sort排序

    在用table.sort 排序的时候注意,如果使用多个条件排序,应在一个排序函数里按照条件优先级进行比较排序. 例如 local t = { {time = , i = }, {time = , i ...

  2. Lua 数组排序 table.sort的注意事项

    1. table中不能有nil table.sort是排序函数,它要求要排序的目标table的必须是从1到n连续的,即中间不能有nil. 2. 重写的比较函数,两个值相等时不能return true ...

  3. Table.Sort排序…Sort(Power Query 之 M 语言)

    数据源: 任意查询表 目标: 对其中一列数据进行排序 操作过程: 选取对象>[主页]>[排序]>[升序排序] 选取对象>[主页]>[排序]>[降序排序] M公式: ...

  4. lua的table.sort

    local aa = {{a=11},{a=44},{a=33},{a=2} } table.sort(aa,function(a,b) return a.a>b.a end) for k, v ...

  5. lua 排序table.sort()用法

    table.sort(),它要求要排序的目标table的必须是从1到n连续的,即中间不能有nil.当两个数相等的时候,比较函数一定要返回false. 探究性质,我们做个试验: 1)新建文件sortte ...

  6. lua的table排序

    lua中利用到的排序的基本上就是构造函数(table)了,为了便于和C区分开来,我俗称它为表单. 实例:(原理就是LUA集成的冒泡算法) 排序的一般姿势(对于只包含数字或者只包含字符串的简单数组) t ...

  7. (转)Lua的table库函数insert、remove、concat、sort详细介绍

    原帖链接:http://www.jb51.net/article/64711.htm#comments 有增注标识的地方为额外注释,非原帖内容. 函数列表:(增注:只能用于数组!) table.ins ...

  8. Lua的table库函数insert、remove、concat、sort详细介绍(转载)

    函数列表: table.insert(table,[ pos,] value) table.remove(table[, pos]) table.concat(table[, sep[, i[, j] ...

  9. LUA table.sort的问题,数组与表的区别

    t = { [] = , [] = , [] = , [] = , } t1 = { , , , , } t2 = { 'a', 'b','d','c', } function cmp(v1, v2) ...

随机推荐

  1. Django-model基础

    Django-model基础 在Django-ORM中表和类存在映射关系 表名<------------>类名 字段<------------>属性 表记录<------ ...

  2. MyBatis - 7.MyBatis逆向 Generator

    MyBatis Generator: 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类.支持基本的增删改查,以及QBC风格的条 ...

  3. JCenter下载太慢, jcenter修改 https为http也许能帮助你

    今天导入一个工程到studio,一直卡在下载那块. 看到下载地址是:https://jcenter.bintray.com/........https!!!! 到浏览器下载,果然也下载不下来.. 于是 ...

  4. 494. Target Sum

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  5. webpack学习笔记--配置module

    Module module 配置如何处理模块. 配置 Loader rules  配置模块的读取和解析规则,通常用来配置 Loader.其类型是一个数组,数组里每一项都描述了如何去处理部分文件. 配置 ...

  6. [转] 最详尽的 JS 原型与原型链终极详解

    四. __proto__ JS 在创建对象(不论是普通对象还是函数对象)的时候,都有一个叫做__proto__ 的内置属性,用于指向创建它的构造函数的原型对象. 对象 person1 有一个 __pr ...

  7. bzoj2961 共点圆 bzoj 4140

    题解: 比较水的一道题 首先我们化简一下式子发现是维护xxo+yyo的最值 显然是用凸包来做 我们可以直接用支持插入删除的凸包 也是nlogn的 因为没有强制在线,我们也可以cdq,考虑前面一半对答案 ...

  8. redis centos 6.5 redis版本3.2.8安装过程

    redis作为非关系数据库的典型应用,在庞大的数据通信处理有着自己强大的优势,今天也自己来开始学些redis. 以下每一个语句都是我执行的命令. 按照所查资料分析,需要tcl测试工具,这个在cento ...

  9. eclipse 编辑代码区字体大小

    wiondow-->preferences-->general-->appearance-->colors and fonts-->java-->java edit ...

  10. Centos7防火墙常用命令及mask锁定不能添加端口问题

    一.开放端口 sudo firewall-cmd --zone=public --add-port=3000/tcp --permanent sudo firewall-cmd --reload 二. ...