From 2c76b7dde0e4d704ca1b35e7245b719064e2675c Mon Sep 17 00:00:00 2001 From: Max Cahill <1bardesign@gmail.com> Date: Tue, 6 Dec 2022 10:45:57 +1100 Subject: [PATCH] fixed crash in spairs and then provided a more robust default_less function which sorts numbers before strings, but sorts those groups with operator< --- sort.lua | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/sort.lua b/sort.lua index b05fe02..ab51f32 100644 --- a/sort.lua +++ b/sort.lua @@ -100,16 +100,26 @@ end local _sorted_types = { --a list of types that will be sorted by default_less --provide a custom sort function to sort other types - ["string"] = true, - ["number"] = true, + ["number"] = 1, + ["string"] = 2, } local function default_less(a, b) - if not _sorted_types[type(a)] or not _sorted_types[type(b)] then + local sort_a = _sorted_types[type(a)] + local sort_b = _sorted_types[type(b)] + if not sort_a or not sort_b then return false end + --different types, sorted by type + if sort_a ~= sort_b then + return sort_a < sort_b + end + --otherwise same type, use less return a < b end +--export it so others can use it +sort.default_less = default_less + --inline common setup stuff function sort._sort_setup(array, less) --default less