Comparison

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
comparison
equal deleted inserted replaced
9326:c9c4b8bc53b1 9327:f6f1dec164b5
175 t_insert(t, var); 175 t_insert(t, var);
176 end 176 end
177 return t; 177 return t;
178 end 178 end
179 179
180 function it.sorted_pairs(t, sort_func)
181 local keys = it.to_array(it.keys(t));
182 table.sort(keys, sort_func);
183 local i = 0;
184 return function ()
185 i = i + 1;
186 local key = keys[i];
187 if key ~= nil then
188 return key, t[key];
189 end
190 end;
191 end
192
180 -- Treat the return of an iterator as key,value pairs, 193 -- Treat the return of an iterator as key,value pairs,
181 -- and build a table 194 -- and build a table
182 function it.to_table(f, s, var) 195 function it.to_table(f, s, var)
183 local t, var2 = {}; 196 local t, var2 = {};
184 while true do 197 while true do