From ebda8931ab57b0467d1b155c5f470454d8d2dd6d Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Fri, 12 Aug 2016 20:09:46 +0200 Subject: Add simple sort settings --- API.md | 5 +++++ init.lua | 15 +++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/API.md b/API.md index d906e95..4dea73a 100644 --- a/API.md +++ b/API.md @@ -53,6 +53,11 @@ data. will be shown as tooltip. Recommended style (in English): First letter capitalized, no puncation at end of sentence, max. 100 characters + * `sorting`: (optional) Sorting method of entries + * `"abc"`: Alphabetical (default) + * `"nosort": Entries appear in no particular order + * A table: Entries appear in exactly the same order as specified in the + table of entry IDs * `build_formspec`: The template function. Takes entry data as its only parameter (has the data type of the entry data) and must return a formspec which is inserted in the Entry tab. diff --git a/init.lua b/init.lua index c0e02aa..2fc7917 100644 --- a/init.lua +++ b/init.lua @@ -256,7 +256,8 @@ 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 cat = doc.data.categories[cid] + for eid,entry in pairs(cat.entries) do local new_entry = table.copy(entry) new_entry.eid = eid table.insert(entry_table, new_entry) @@ -267,10 +268,16 @@ function doc.get_sorted_entry_names(cid) 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 + -- Sorting algorithm + local comp + if cat.def.sorting ~= "nosort" then + if cat.def.sorting == "abc" or cat.def.sorting == nil then + 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) + end end - table.sort(entry_table, comp) return entry_table end -- cgit v1.2.3