Software /
code /
prosody
Diff
util/iterators.lua @ 9327:f6f1dec164b5
util.iterators: Add sorted_pairs() method
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 21 Sep 2018 14:27:46 +0100 |
parent | 8797:7b621a4a2e8d |
child | 9688:eade1316728e |
line wrap: on
line diff
--- a/util/iterators.lua Mon Sep 17 15:28:53 2018 +0100 +++ b/util/iterators.lua Fri Sep 21 14:27:46 2018 +0100 @@ -177,6 +177,19 @@ 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)