diff options
author | Matthew Wild <mwild1@gmail.com> | 2018-09-21 14:27:46 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2018-09-21 14:27:46 +0100 |
commit | 5947c028a9e5d586796b273538795bc1cc14785e (patch) | |
tree | 24eec4723dfbfd740e1a6bc75f204e7066265922 /util/iterators.lua | |
parent | 74cb650790e018d95e8f1cb3735b369620845c22 (diff) | |
download | prosody-5947c028a9e5d586796b273538795bc1cc14785e.tar.gz prosody-5947c028a9e5d586796b273538795bc1cc14785e.zip |
util.iterators: Add sorted_pairs() method
Diffstat (limited to 'util/iterators.lua')
-rw-r--r-- | util/iterators.lua | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/util/iterators.lua b/util/iterators.lua index 5d16d8c1..302cca36 100644 --- a/util/iterators.lua +++ b/util/iterators.lua @@ -177,6 +177,19 @@ function it.to_array(f, s, var) return t; end +function it.sorted_pairs(t, sort_func) + local keys = it.to_array(it.keys(t)); + table.sort(keys, sort_func); + local i = 0; + return function () + i = i + 1; + local key = keys[i]; + if key ~= nil then + return key, t[key]; + end + end; +end + -- Treat the return of an iterator as key,value pairs, -- and build a table function it.to_table(f, s, var) |