Improved sorting of tables.
This commit is contained in:
parent
ec3ba2cab9
commit
5a3e9d0e27
34
pretty.lua
34
pretty.lua
|
@ -37,7 +37,17 @@ local SINGLE_LINE_SEQ_MAX_ELEMENTS = 10
|
|||
local SINGLE_LINE_MAP_MAX_ELEMENTS = 5
|
||||
local NR_CHARS_IN_LONG_STRING = 40
|
||||
|
||||
local TYPE_SORT_ORDER = {
|
||||
local KEY_TYPE_SORT_ORDER = {
|
||||
['number'] = 0,
|
||||
['string'] = 1,
|
||||
['boolean'] = 2,
|
||||
['table'] = 3,
|
||||
['userdata'] = 4,
|
||||
['thread'] = 5,
|
||||
['function'] = 6,
|
||||
}
|
||||
|
||||
local VALUE_TYPE_SORT_ORDER = {
|
||||
['nil'] = 0,
|
||||
['boolean'] = 1,
|
||||
['number'] = 2,
|
||||
|
@ -118,23 +128,27 @@ local function compare_key_value_pairs (a, b)
|
|||
local type_value_a, type_value_b = type(a[2]), type(b[2])
|
||||
|
||||
-- Tons of compare
|
||||
if (type_key_a == 'string' and type_key_b == 'string') then
|
||||
return alphanum_compare_strings(a[1], b[1])
|
||||
if (1 == (type_key_a == 'number' and 1 or 0) + (type_key_b == 'number' and 1 or 0)) then
|
||||
return type_key_a == 'number'
|
||||
elseif (type_key_a == 'number' and type_key_b == 'number') then
|
||||
return a[1] < b[1]
|
||||
else
|
||||
return TYPE_SORT_ORDER[type_value_a] < TYPE_SORT_ORDER[type_value_b]
|
||||
elseif (type_value_a ~= type_value_b) then
|
||||
return VALUE_TYPE_SORT_ORDER[type_value_a] < VALUE_TYPE_SORT_ORDER[type_value_b]
|
||||
elseif (type_key_a == 'string' and type_key_b == 'string') then
|
||||
return alphanum_compare_strings(a[1], b[1])
|
||||
elseif (type_key_a ~= type_key_b) then
|
||||
return KEY_TYPE_SORT_ORDER[type_value_a] < KEY_TYPE_SORT_ORDER[type_value_b]
|
||||
end
|
||||
end
|
||||
|
||||
local function get_key_value_pairs_in_proper_order (t)
|
||||
-- Generates a sequence of key value pairs, in proper order.
|
||||
-- Proper order is:
|
||||
-- 1. By value type: as defined by the TYPE_SORT_ORDER in the top.
|
||||
-- 2. By key type: TODO: Implement this.
|
||||
-- 2.1. Numbers
|
||||
-- 2.2. Strings in alphanumeric order
|
||||
-- 2.3. Other wierdness.
|
||||
-- 1. All integer keys are first, in order
|
||||
-- 2. Then by value type, as defined in VALUE_TYPE_SORT_ORDER in the top.
|
||||
-- 3. Then by key type.
|
||||
-- 3.1. String in alphanumeric order
|
||||
-- 3.2. Other wierdness.
|
||||
|
||||
local key_value_pairs = {}
|
||||
|
||||
|
|
|
@ -284,7 +284,7 @@ format_test {
|
|||
|
||||
format_test {
|
||||
input = { [true] = 1, [1] = false },
|
||||
expect = '{\n\t[true] = 1,\n\t[1] = false\n}',
|
||||
expect = '{\n\t[1] = false,\n\t[true] = 1\n}',
|
||||
}
|
||||
|
||||
format_test {
|
||||
|
@ -361,6 +361,16 @@ format_test {
|
|||
expect = '{ {}, true, false, 5 }',
|
||||
}
|
||||
|
||||
format_test {
|
||||
input = { [1] = {}, [2] = {}, [3] = {}, ['down_info'] = {}, ['up_info'] = {} },
|
||||
expect = '{\n\t[1] = {},\n\t[2] = {},\n\t[3] = {},\n\t[\'down_info\'] = {},\n\t[\'up_info\'] = {}\n}',
|
||||
}
|
||||
|
||||
format_test {
|
||||
input = { 'hello', 123, {}, true },
|
||||
expect = '{ \'hello\', 123, {}, true }',
|
||||
}
|
||||
|
||||
-- TODO: Add more tests for sorting.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in New Issue
Block a user