comparison sort.lua @ 0:ec731d2fe6ba

Version 1.2.12.0
author Adam tegen <adam.tegen@gmail.com>
date Tue, 20 May 2014 21:43:23 -0500
parents
children e77e01abce98
comparison
equal deleted inserted replaced
-1:000000000000 0:ec731d2fe6ba
1 local _, AskMrRobot = ...
2
3 function AskMrRobot.spairs(t, order)
4 -- collect the keys
5 local keys = {}
6 for k in pairs(t) do keys[#keys+1] = k end
7
8 -- if order function given, sort by it by passing the table and keys a, b,
9 -- otherwise just sort the keys
10 if order then
11 table.sort(keys, function(a,b) return order(t, a, b) end)
12 else
13 table.sort(keys)
14 end
15
16 -- return the iterator function
17 local i = 0
18 return function()
19 i = i + 1
20 if keys[i] then
21 return keys[i], t[keys[i]]
22 end
23 end
24 end
25
26 function AskMrRobot.sortSlots(t)
27 return AskMrRobot.spairs(t, function(x, a, b)
28 if a == nil and b == nil then return 0 end
29 return AskMrRobot.sortedSlots[a] < AskMrRobot.sortedSlots[b]
30 end)
31 end