From 1a02819a668b7ddaa68bafedf5ec53d5e56f6670 Mon Sep 17 00:00:00 2001 From: Simon Hirsig Date: Fri, 16 Oct 2015 13:25:31 +0200 Subject: [PATCH] added group by utility --- util.lua | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/util.lua b/util.lua index 8347433..3c152d5 100644 --- a/util.lua +++ b/util.lua @@ -234,4 +234,18 @@ function Aux.util.money_string(money) return format(GSC_1, c) end end -end \ No newline at end of file +end + +function Aux.util.group_by(tables, keys) + local groups = {} + Aux.util.map(tables, function(table) + for _, group in ipairs(groups) do + if Aux.util.all(keys, function(key) table[key] == group[1][key] end) then + tinsert(group, table) + return + end + end + tinsert(groups, { table }) + end) + return groups +end