From 5a3e9d0e27de677206b45168f92d5bf3fdb6dfb0 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Wed, 5 Apr 2017 13:10:23 +0200 Subject: [PATCH] Improved sorting of tables. --- pretty.lua | 34 ++++++++++++++++++++++++---------- test/test_pretty.lua | 12 +++++++++++- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/pretty.lua b/pretty.lua index 3ca5aa6..a514ea5 100644 --- a/pretty.lua +++ b/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 = {} diff --git a/test/test_pretty.lua b/test/test_pretty.lua index 7bf2bd6..422fc29 100644 --- a/test/test_pretty.lua +++ b/test/test_pretty.lua @@ -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. --------------------------------------------------------------------------------