Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a Table[] in Lua

I have a Lua table that I am trying to sort. The table's format is as follows:

tableOfKills[PlayerName] = NumberOfKills 

Which means, for example, if I had a player named Robin with a total of 8 kills and another named Jon with a total of 10 kills, the table would be:

tableOfKills[Robin] = 8 tableOfKills[Jon]   = 10 

How would I sort that type of table to show the highest kills first? Thanks in advance!

like image 429
sgtaziz Avatar asked Mar 29 '13 15:03

sgtaziz


People also ask

How do you sort a Lua table?

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.

Is Lua table sort stable?

"[Table. sort] algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort." Any ideas when Table.

What sorting algorithm does Lua use?

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

What is Lua table?

Tables are the only data structure available in Lua that helps us create different types like arrays and dictionaries. Lua uses associative arrays and which can be indexed with not only numbers but also with strings except nil. Tables have no fixed size and can grow based on our need.


1 Answers

A table in Lua is a set of key-value mappings with unique keys. The pairs are stored in arbitrary order and therefore the table is not sorted in any way.

What you can do is iterate over the table in some order. The basic pairs gives you no guarantee of the order in which the keys are visited. Here is a customized version of pairs, which I called spairs because it iterates over the table in a sorted order:

function spairs(t, order)     -- collect the keys     local keys = {}     for k in pairs(t) do keys[#keys+1] = k end      -- if order function given, sort by it by passing the table and keys a, b,     -- otherwise just sort the keys      if order then         table.sort(keys, function(a,b) return order(t, a, b) end)     else         table.sort(keys)     end      -- return the iterator function     local i = 0     return function()         i = i + 1         if keys[i] then             return keys[i], t[keys[i]]         end     end end 

Here is an example of use of such function:

HighScore = { Robin = 8, Jon = 10, Max = 11 }  -- basic usage, just sort by the keys for k,v in spairs(HighScore) do     print(k,v) end --> Jon     10 --> Max     11 --> Robin   8  -- this uses an custom sorting function ordering by score descending for k,v in spairs(HighScore, function(t,a,b) return t[b] < t[a] end) do     print(k,v) end --> Max     11 --> Jon     10 --> Robin   8 
like image 69
Michal Kottman Avatar answered Sep 19 '22 22:09

Michal Kottman