summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--libs/core/luasrc/util.lua37
1 files changed, 37 insertions, 0 deletions
diff --git a/libs/core/luasrc/util.lua b/libs/core/luasrc/util.lua
index 11f77206b..ad53138e6 100644
--- a/libs/core/luasrc/util.lua
+++ b/libs/core/luasrc/util.lua
@@ -318,3 +318,40 @@ function parse_units(ustr)
return val
end
+
+
+-- Provide various sorting iterators
+function _sortiter( t, f )
+ local keys = { }
+
+ for k, v in pairs(t) do
+ table.insert( keys, k )
+ end
+
+ local _pos = 0
+ local _len = table.getn( keys )
+
+ table.sort( keys, f )
+
+ return function()
+ _pos = _pos + 1
+ if _pos <= _len then
+ return keys[_pos], t[keys[_pos]]
+ end
+ end
+end
+
+-- Return key, value pairs sorted by provided callback function
+function spairs(t,f)
+ return _sortiter( t, f )
+end
+
+-- Return key, value pairs sorted by keys
+function kspairs(t)
+ return _sortiter( t )
+end
+
+-- Return key, value pairs sorted by values
+function vspairs(t)
+ return _sortiter( t, function (a,b) return t[a] < t[b] end )
+end