mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-24 22:54:35 +00:00
fixed crash in spairs and then provided a more robust default_less function which sorts numbers before strings, but sorts those groups with operator<
This commit is contained in:
parent
0d88061e3b
commit
2c76b7dde0
16
sort.lua
16
sort.lua
@ -100,16 +100,26 @@ end
|
|||||||
local _sorted_types = {
|
local _sorted_types = {
|
||||||
--a list of types that will be sorted by default_less
|
--a list of types that will be sorted by default_less
|
||||||
--provide a custom sort function to sort other types
|
--provide a custom sort function to sort other types
|
||||||
["string"] = true,
|
["number"] = 1,
|
||||||
["number"] = true,
|
["string"] = 2,
|
||||||
}
|
}
|
||||||
local function default_less(a, b)
|
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
|
return false
|
||||||
end
|
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
|
return a < b
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--export it so others can use it
|
||||||
|
sort.default_less = default_less
|
||||||
|
|
||||||
--inline common setup stuff
|
--inline common setup stuff
|
||||||
function sort._sort_setup(array, less)
|
function sort._sort_setup(array, less)
|
||||||
--default less
|
--default less
|
||||||
|
Loading…
Reference in New Issue
Block a user