Add simple sort settings

This commit is contained in:
Wuzzy 2016-08-12 20:09:46 +02:00
parent 57bfa80e4b
commit ebda8931ab
2 changed files with 16 additions and 4 deletions

5
API.md
View File

@ -53,6 +53,11 @@ data.
will be shown as tooltip. Recommended style (in English): will be shown as tooltip. Recommended style (in English):
First letter capitalized, no puncation at end of sentence, First letter capitalized, no puncation at end of sentence,
max. 100 characters 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 * `build_formspec`: The template function. Takes entry data as its
only parameter (has the data type of the entry data) and must only parameter (has the data type of the entry data) and must
return a formspec which is inserted in the Entry tab. return a formspec which is inserted in the Entry tab.

View File

@ -256,7 +256,8 @@ end
function doc.get_sorted_entry_names(cid) function doc.get_sorted_entry_names(cid)
local sort_table = {} local sort_table = {}
local entry_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) local new_entry = table.copy(entry)
new_entry.eid = eid new_entry.eid = eid
table.insert(entry_table, new_entry) table.insert(entry_table, new_entry)
@ -267,10 +268,16 @@ function doc.get_sorted_entry_names(cid)
for i=1, #sort_table do for i=1, #sort_table do
reverse_sort_table[sort_table[i]] = i reverse_sort_table[sort_table[i]] = i
end end
local comp = function(e1, e2) -- Sorting algorithm
if reverse_sort_table[e1.name] < reverse_sort_table[e2.name] then return true else return false end 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 end
table.sort(entry_table, comp)
return entry_table return entry_table
end end