diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sort.lua	Tue May 20 21:43:23 2014 -0500
@@ -0,0 +1,31 @@
+local _, AskMrRobot = ...
+
+function AskMrRobot.spairs(t, order)
+    -- collect the keys
+    local keys = {}
+    for k in pairs(t) do keys[#keys+1] = k end
+
+    -- if order function given, sort by it by passing the table and keys a, b,
+    -- otherwise just sort the keys 
+    if order then
+        table.sort(keys, function(a,b) return order(t, a, b) end)
+    else
+        table.sort(keys)
+    end
+
+    -- return the iterator function
+    local i = 0
+    return function()
+        i = i + 1
+        if keys[i] then
+            return keys[i], t[keys[i]]
+        end
+    end
+end
+
+function AskMrRobot.sortSlots(t)
+    return AskMrRobot.spairs(t, function(x, a, b)
+        if a == nil and b == nil then return 0 end
+        return AskMrRobot.sortedSlots[a] < AskMrRobot.sortedSlots[b]
+    end)
+end