From d6cecb387c867f14a3a9de793e158c874a974253 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sat, 13 Aug 2016 13:30:38 +0200 Subject: Allow function-based sorting --- API.md | 21 ++++++++++++++++----- init.lua | 7 +++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/API.md b/API.md index 636263a..91dfe87 100644 --- a/API.md +++ b/API.md @@ -57,16 +57,27 @@ data. * `"abc"`: Alphabetical (default) * `"nosort"`: Entries appear in no particular order * `"custom"`: Manually define the order of entries in `sorting_data` + * `"function"`: Sort by function defined in `sorting_data` * `sorting_data`: Additional data for special sorting methods. - If `sorting=="custom"`, this field must contain a table (list form) in which - the entry IDs are specified in the order they are supposed to appear in the - entry list. All entries which are missing in this table will appear in no - particular order below the final specified one. - This field is not required if `sorting` has any other value + * If `sorting=="custom"`, this field must contain a table (list form) in which + the entry IDs are specified in the order they are supposed to appear in the + entry list. All entries which are missing in this table will appear in no + particular order below the final specified one. + * If `sorting=="function"`, this field is a compare function to be used as + the `comp` parameter of `table.sort`. The parameters given are two entries. + * This field is not required if `sorting` has any other value * `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. +Note: For function-based sorting, the entries provided in the compare function have the +following format: + + { + name = n, -- entry name + data = d, -- arbitrary entry data + } + #### Using `build_formspec` For `build_formspec` you can either define your own function which procedurally generates the entry formspec or you use one of the diff --git a/init.lua b/init.lua index 6c12494..d7f0860 100644 --- a/init.lua +++ b/init.lua @@ -286,13 +286,16 @@ function doc.get_sorted_entry_names(cid) end local comp if cat.def.sorting ~= "nosort" then + -- Sorting by user function + if cat.def.sorting == "function" then + comp = cat.def.sorting_data -- Alphabetic sorting - if cat.def.sorting == "abc" or cat.def.sorting == nil then + elseif 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 + table.sort(entry_table, comp) end return entry_table -- cgit v1.2.3