5

I have the below program code which tries to sort a given list. I have tried various options and it still doesn`t work.

local List = {}    
List[143] = "143"    
List[145] = "145"    
List[120] = "120"       
List[178] = "178"   
table.sort(List, compare)

compare function is defined as

function compare(a, b)    
    if tonumber(a) < tonumber(b) then    
        return true    
    end
end

Above table.sort does not work to any order. I just want to sort it to increasing numerical order. If you have any ideas about this please help me. Thanks in advance

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
user2951607
  • 61
  • 1
  • 3
  • 3
    your example is sorted already, do you expect the keys or the values sorted? + see http://www.lua.org/pil/19.3.html – Dmitry Ledentsov Jan 14 '14 at 14:08
  • 3
    Your compare function could be simplified to just `return tonumber(a) < tonumber(b)`. – Ryan Stein Jan 14 '14 at 17:32
  • 1
    Sorting means to reorder values referenced by a ordered set of keys. Which keys do you intend to use? Or, more specifically, what is the outcome that you expect in your example? – Tom Blodget Jan 15 '14 at 02:42

3 Answers3

12

table.sort (and much of the rest of the table.* functions) is defined only for operations on array-like tables. That means tables with contiguous integer keys from 1..n. Your table doesn't meet those criteria.

RBerteig
  • 41,948
  • 7
  • 88
  • 128
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
3

The keys of that table do not satisfy requirements of that function. The keys must start at 1 and sequentially increase to N, as per Lua ref manual ("Sorts table elements in a given order, in-place, from table[1] to table[n]"). Try

local List = {}
List[1] = "143"    
List[2] = "145"    
List[3] = "120"       
List[4] = "178"   
table.sort(List, compare)

or even better

local List = {"143", "145", "120", "178"}
table.sort(List, compare)
RBerteig
  • 41,948
  • 7
  • 88
  • 128
Oliver
  • 27,510
  • 9
  • 72
  • 103
0

Key-value pairs in a table don't have a stable order. For each run through with the pairs or next functions, you may see a different sequence of key-value pairs. (Presumably the sequence might change as a result of adding or removing from the table.)

Except for setting or getting at specific keys, other table operations use the positive integer keys. maxn gets the maximum key n that does not have a nil value. The others assume the keys are contiguous. For such tables, the positive integer keys can be considered to have a definite range and are ordered so sorting would make sense.

The positive integer keys in List are not contiguous. So, sort's behavior is not useful (and, in general, non-deterministic).

Perhaps, you wanted something like this operation over all positive integer keys:

local values = {}
-- extract values from positive integer keys
for key, value in pairs(List)
    if (type(key) = "number") and key > 0 then
        table.insert(values, value)
        List[key] = nil    
    end
end
-- sort and restore them to the table
table.sort(values, function(a,b) return tonumber(a)<tonumber(b) end)
for key, value in values
    List[key] = value
end
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
  • That isn't modern Lua... it would have to read `for key,value in pairs(List)` at the least, and the `.maxn` field is generally not present in Lua 5.1 and newer. – RBerteig Jan 16 '14 at 01:07
  • Thanks. I meant to call `pairs`. `maxn` is valid in 5.1 but not 5.2. – Tom Blodget Jan 16 '14 at 02:17