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) ...
随机推荐
- jenkins上节点显示swap空间不足解决方案
查看内存占用情况:free -m 1.swap分区原理: swap分区在系统的物理内存不够用的时候,把物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时 ...
- 集腋成裘-01-html -html基础
1 标签 1.1 单标签 注释标签 <!-- 注释标签 --> 换行标签 <br/> 水平线 <hr/> <img src="图片来源" ...
- Controller中方法返回值其他类型需要添加jackson依赖
第一个 第二个: 第三个 https://www.cnblogs.com/codejackanapes/p/5569013.html:json的博客园 springmvc默认的是:2.Jackson: ...
- ReactNative——页面跳转
效果图: 进入工作目录,运行 react-native init NavigatorProject 创建项目NavigatorProject import React, { Component } f ...
- The authenticity of host 'slaver2 (192.168.199.132)' can't be established. RSA key fingerprint is cc:4e:23:01:ca:97:52:21:85:78:bc:29:ca:b3:12:52.
1:ssh登录 The authenticity of host 192.168.199.132 can't be established. 的问题 问题出现了,总要解决吧,百度一下,详细介绍的很多, ...
- .Net Core下发送WebRequest请求的两种方式
1.使用RestSharp.NetCore 2.使用WebApi请求方式
- 键盘Hook【Delphi版】
原文:https://www.cnblogs.com/edisonfeng/archive/2012/05/18/2507858.html 一.钩子的基本概念 a) Hook作用:监视windows消 ...
- HashMap、HashTable与ConcurrentHashMap区别
线程不安全的HashMap 在多线程环境下,使用HashMap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap.例如,执行如下代码会引起死循环. fin ...
- 51Nod1309 Value of all Permutations 期望
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1309.html 题目传送门 - 51Nod1309 题意 长度为N的整数数组A,有Q个查询,每个查询 ...
- Codeforces 982E Billiard 扩展欧几里德
原文链接http://www.cnblogs.com/zhouzhendong/p/9055728.html 题目传送门 - Codeforces 928E 题意 一束与坐标轴平行或者成$45^\ci ...