Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a Lua table by key

I have gone through many questions and Google results but couldn't find the solution.

I am trying to sort a table using table.sort function in Lua but I can't figure out how to use it.

I have a table that has keys as random numeric values. I want to sort them in ascending order. I have gone through the Lua wiki page also but table.sort only works with the table values.

t = { [223]="asd", [23]="fgh", [543]="hjk", [7]="qwe" }

I want it like:

t = { [7]="qwe", [23]="fgh", [223]="asd", [543]="hjk" }
like image 226
Prakash.DTI Avatar asked Oct 02 '14 11:10

Prakash.DTI


People also ask

How does table sort work Lua?

One of the most used functions in Lua is the sort function which is provided by the Lua library which tables a table as an argument and sorts the values that are present inside the table. The sort function also takes one more argument with the table and that argument is a function which is known as the order function.

What sorting algorithm does Lua use?

The table library provides an in-place sort function, based on the quicksort algorithm [wikipedia].

Are Lua arrays ordered?

Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs , rather than pairs .

Is everything in Lua a table?

Tables are a versatile data structure that can be used as arrays, dictionaries, objects, etc. However, Lua provides many types besides tables: numbers, booleans, closures (functions), strings (which are interned), coroutines, and something called userdata (which are typically pointers to something implemented in C).


2 Answers

You cannot set the order in which the elements are retrieved from the hash (which is what your table is) using pairs. You need to get the keys from that table, sort the keys as its own table, and then use those sorted keys to retrieve the values from your original table:

local t = { [223]="asd", [23]="fgh", [543]="hjk", [7]="qwe" }
local tkeys = {}
-- populate the table that holds the keys
for k in pairs(t) do table.insert(tkeys, k) end
-- sort the keys
table.sort(tkeys)
-- use the keys to retrieve the values in the sorted order
for _, k in ipairs(tkeys) do print(k, t[k]) end

This will print

7   qwe
23  fgh
223 asd
543 hjk

Another option would be to provide your own iterator instead of pairs to iterate the table in the order you need, but the sorting of the keys may be simple enough for your needs.

like image 111
Paul Kulchenko Avatar answered Nov 08 '22 07:11

Paul Kulchenko


What was said by @lhf is true, your lua table holds its contents in whatever order the implementation finds feasible. However, if you want to print (or iterate over it) in a sorted manner, it is possible (so you can compare it element by element). To achieve this, you can do it in the following way

for key, value in orderedPairs(mytable) do
  print(string.format("%s:%s", key, value))
end

Unfortunately, orderedPairs is not provided as a part of lua, you can copy the implementation from here though.

like image 29
vetham Avatar answered Nov 08 '22 06:11

vetham