1
0

Improved sorting of tables.

This commit is contained in:
Jon Michael Aanes 2017-04-05 13:10:23 +02:00
parent ec3ba2cab9
commit 5a3e9d0e27
2 changed files with 35 additions and 11 deletions

View File

@ -37,7 +37,17 @@ local SINGLE_LINE_SEQ_MAX_ELEMENTS = 10
local SINGLE_LINE_MAP_MAX_ELEMENTS = 5 local SINGLE_LINE_MAP_MAX_ELEMENTS = 5
local NR_CHARS_IN_LONG_STRING = 40 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, ['nil'] = 0,
['boolean'] = 1, ['boolean'] = 1,
['number'] = 2, ['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]) local type_value_a, type_value_b = type(a[2]), type(b[2])
-- Tons of compare -- Tons of compare
if (type_key_a == 'string' and type_key_b == 'string') then if (1 == (type_key_a == 'number' and 1 or 0) + (type_key_b == 'number' and 1 or 0)) then
return alphanum_compare_strings(a[1], b[1]) return type_key_a == 'number'
elseif (type_key_a == 'number' and type_key_b == 'number') then elseif (type_key_a == 'number' and type_key_b == 'number') then
return a[1] < b[1] return a[1] < b[1]
else elseif (type_value_a ~= type_value_b) then
return TYPE_SORT_ORDER[type_value_a] < TYPE_SORT_ORDER[type_value_b] 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
end end
local function get_key_value_pairs_in_proper_order (t) local function get_key_value_pairs_in_proper_order (t)
-- Generates a sequence of key value pairs, in proper order. -- Generates a sequence of key value pairs, in proper order.
-- Proper order is: -- Proper order is:
-- 1. By value type: as defined by the TYPE_SORT_ORDER in the top. -- 1. All integer keys are first, in order
-- 2. By key type: TODO: Implement this. -- 2. Then by value type, as defined in VALUE_TYPE_SORT_ORDER in the top.
-- 2.1. Numbers -- 3. Then by key type.
-- 2.2. Strings in alphanumeric order -- 3.1. String in alphanumeric order
-- 2.3. Other wierdness. -- 3.2. Other wierdness.
local key_value_pairs = {} local key_value_pairs = {}

View File

@ -284,7 +284,7 @@ format_test {
format_test { format_test {
input = { [true] = 1, [1] = false }, 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 { format_test {
@ -361,6 +361,16 @@ format_test {
expect = '{ {}, true, false, 5 }', 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. -- TODO: Add more tests for sorting.
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------