Lua的 table.sort排序
在用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排序的更多相关文章
- Lua table.sort排序
在用table.sort 排序的时候注意,如果使用多个条件排序,应在一个排序函数里按照条件优先级进行比较排序. 例如 local t = { {time = , i = }, {time = , i ...
- Lua 数组排序 table.sort的注意事项
1. table中不能有nil table.sort是排序函数,它要求要排序的目标table的必须是从1到n连续的,即中间不能有nil. 2. 重写的比较函数,两个值相等时不能return true ...
- Table.Sort排序…Sort(Power Query 之 M 语言)
数据源: 任意查询表 目标: 对其中一列数据进行排序 操作过程: 选取对象>[主页]>[排序]>[升序排序] 选取对象>[主页]>[排序]>[降序排序] M公式: ...
- 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 ...
- lua 排序table.sort()用法
table.sort(),它要求要排序的目标table的必须是从1到n连续的,即中间不能有nil.当两个数相等的时候,比较函数一定要返回false. 探究性质,我们做个试验: 1)新建文件sortte ...
- lua的table排序
lua中利用到的排序的基本上就是构造函数(table)了,为了便于和C区分开来,我俗称它为表单. 实例:(原理就是LUA集成的冒泡算法) 排序的一般姿势(对于只包含数字或者只包含字符串的简单数组) t ...
- (转)Lua的table库函数insert、remove、concat、sort详细介绍
原帖链接:http://www.jb51.net/article/64711.htm#comments 有增注标识的地方为额外注释,非原帖内容. 函数列表:(增注:只能用于数组!) table.ins ...
- Lua的table库函数insert、remove、concat、sort详细介绍(转载)
函数列表: table.insert(table,[ pos,] value) table.remove(table[, pos]) table.concat(table[, sep[, i[, j] ...
- LUA table.sort的问题,数组与表的区别
t = { [] = , [] = , [] = , [] = , } t1 = { , , , , } t2 = { 'a', 'b','d','c', } function cmp(v1, v2) ...
随机推荐
- linux安装python3+selenium
安装笔记 当前安装使用centos7 安装python3 1.下载 [admin@ ~] wget https://www.python.org/ftp/python/3.6.0/Python-3.6 ...
- python压缩文件
#coding=utf-8 #压缩文件 import os,os.path import zipfile #压缩:传路径,文件名 def zip_compression(dirname,zipfile ...
- k-近邻算法-手写识别系统
手写数字是32x32的黑白图像.为了能使用KNN分类器,我们需要把32x32的二进制图像转换为1x1024 1. 将图像转化为向量 from numpy import * # 导入科学计算包numpy ...
- 华硕X75VB安装ubuntu12.10网卡不可用等相关问题总结
笔记本相关信息: 电脑型号:华硕X75VB 笔记本电脑 处理器:i5-3230M 2.60GHz 双核 主板:华硕X75VB (英特尔 Ivy Bridge - HM76 Express芯片组) 内存 ...
- 解决OS睡眠功能中,移动鼠标就会唤醒
设备管理器,在相应项目上右键属性.
- 将现有项目添加到TFS中
假设在Projects文件夹中有一个名为WpfApplication1的项目需要添加到TFS. 我们可以这样做: 1.打开视图->团队资源管理器,点击管理连接,在弹出的窗口中选择服务器和团队项目 ...
- Give root password for maintenance(or type control -D to continue)
2017-09-30 18:12:08 1:错误如图,本来开机准备用一下虚拟机,就出现一个这,为啥记录一下呢,因为网上好多不是很靠谱. 原因可能是之前关闭虚拟机的时候不小心出现异常了: 2:解决办法: ...
- JavaScript 匿名函数
转载自:https://www.cnblogs.com/ClareZjy/p/6365891.html 零:介绍 匿名函数的基本形式为(function(){...})(); 前面的括号包含函数体 ...
- centos7 yum install redis
直接yum 安装的redis 不是最新版本 yum install redis 如果要安装最新的redis,需要安装Remi的软件源,官网地址:http://rpms.famillecollet.co ...
- 【C#】使用OWIN创建Web API
OWIN的介绍 OWIN 的全称是 "Open Web Interface for .NET", OWIN 在 .NET Web 服务器和 .NET Web 应用之间定义了一套标准 ...