function sort(list, ...)
local opts = {...};
local len = #opts;
return table.sort(list, function(a, b)
return comp(a, b, opts, 1, len);
end);
end function comp(a, b, opts, i, len)
if (len >= i) then
local o = opts[i];
local k = o[1];
local asc = o[2] or true;
if (a[k] < b[k] == asc) then return true end;
if (a[k] == b[k]) then return comp(a, b, opts, i+1, len) end;
return false;
end
return false;
end local l = {
{x=1,y=2,z=1},
{x=1,y=1,z=4},
{x=1,y=4,z=3},
{x=2,y=2,z=6},
{x=1,y=2,z=3},
{x=1,y=2,z=3},
{x=2,y=5,z=3},
{x=3,y=4,z=3},
{x=1,y=2,z=6},
{x=2,y=1,z=3},
{x=3,y=2,z=3}, }
sort(l, {"x",true},{"y",true},{"z",true});
for k,v in ipairs(l) do
print(k,v.x,v.y,v.z);
end
1	1	1	4
2 1 2 1
3 1 2 3
4 1 2 3
5 1 2 6
6 1 4 3
7 2 1 3
8 2 2 6
9 2 5 3
10 3 2 3
11 3 4 3

非递归版本

function sort(list, ...)
local opts = {...};
return table.sort(list, function(a, b)
for i, v in ipairs(opts) do
local k = v[1];
local asc = v[2] or true;
if (a[k] < b[k] == asc) then return true end;
if (a[k] ~= b[k]) then return false end;
end
return false;
end);
end

最新文章

  1. C#_基础:委托速讲
  2. What is “:-!!” in C code?
  3. nginx认证配置
  4. Learning jQuery, 4th Edition 勘误表
  5. ubuntu下firefox浏览器flash player插件的安装
  6. 使用BeanUtils工具类操作Java bean
  7. C++对象的自销毁
  8. 使用Httpwatch分析响应时间--转
  9. DOM元素拖拽效果
  10. mssql sqlserver isnull coalesce函数用法区别说明
  11. 用c# 开发html5的尝试,试用bridge.net
  12. day06 数字类型,字符串类型,列表类型
  13. ES6 迭代器和生成器
  14. $(&quot;).each 和$.each
  15. 转【面向代码】学习 Deep Learning(二)Deep Belief Nets(DBNs)
  16. Java 8 实战
  17. SharePoint 站点导航Web部件
  18. python 标准库简介
  19. kotlin 遇到的问题
  20. [Winter Vacation] 守护,守望

热门文章

  1. grep展示相邻行
  2. Python-celery介绍与快速上手
  3. easyui subGrid实现
  4. mac os 11 Big Sur 根目录无法写入解决办法
  5. ES得分
  6. 对LVDS的浅显理解
  7. Echarts实现不均匀刻度的方法,自定义刻度(转)
  8. 更改svn地址
  9. Codeforces Round #769 (Div. 2) - D. New Year Concert
  10. plsql和instantclient版本都对,依然不能初始化oci.dll解决办法