summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWuzzy <almikes@aol.com>2015-05-16 21:02:34 +0200
committerWuzzy <almikes@aol.com>2015-05-16 21:02:34 +0200
commit4fc84edb8a76a9e512dfc7d4fb80dbd1011e1b69 (patch)
tree1226a1ec8433de5bf3dbb5d3563a363a8c34cfe4
parent4b052c050fe935e9a024f8228f76e9e879eb9e05 (diff)
Automatically sort entries by name
-rw-r--r--init.lua29
1 files changed, 26 insertions, 3 deletions
diff --git a/init.lua b/init.lua
index 63821fd..2e55a4e 100644
--- a/init.lua
+++ b/init.lua
@@ -58,9 +58,10 @@ function doc.generate_entry_list(cid, playername)
local entry_textlist = "textlist[0,1;11,7;doc_catlist;"
local counter = 0
doc.data.players[playername].entry_ids = {}
- for eid,entry in pairs(doc.data.categories[cid].entries) do
- table.insert(doc.data.players[playername].entry_ids, eid)
- entry_textlist = entry_textlist .. entry.name .. ","
+ local entries = doc.get_sorted_entry_names(cid)
+ for i=1, #entries do
+ table.insert(doc.data.players[playername].entry_ids, entries[i].eid)
+ entry_textlist = entry_textlist .. entries[i].name .. ","
counter = counter + 1
end
if counter >= 1 then
@@ -79,6 +80,28 @@ function doc.generate_entry_list(cid, playername)
return formstring
end
+function doc.get_sorted_entry_names(cid)
+ local sort_table = {}
+ local entry_table = {}
+ for eid,entry in pairs(doc.data.categories[cid].entries) do
+ local new_entry = table.copy(entry)
+ new_entry.eid = eid
+ table.insert(entry_table, new_entry)
+ table.insert(sort_table, entry.name)
+ end
+ table.sort(sort_table)
+ local reverse_sort_table = table.copy(sort_table)
+ for i=1, #sort_table do
+ reverse_sort_table[sort_table[i]] = i
+ end
+ local comp = function(e1, e2)
+ if reverse_sort_table[e1.name] < reverse_sort_table[e2.name] then return true else return false end
+ end
+ table.sort(entry_table, comp)
+
+ return entry_table
+end
+
function doc.formspec_category(id, playername)
local formstring
if id == nil then