Sorting arrays

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
danabnormal
Posts: 1
Joined: Tue May 09, 2006 4:01 am

Sorting arrays

Post by danabnormal »

Hi chaps!

I'm just thinking about my next project all possibles require one basic function - sorting arrays.

Is there a built in command for sorting or querying arrays, or do you rely on custom built procedures for this?

Thanks!

danabnormal
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Table.sort(table, function (a, b) return a > b end)


http://www.lua.org/manual/5.0/manual.html#2.5.6
table.sort (table [, comp])
Sorts table elements in a given order, in-place, from table[1] to table[n], where n is the size of the table (see 5.4). If comp is given, then it must be a function that receives two table elements, and returns true when the first is less than the second (so that not comp(a[i+1],a) will be true after the sort). If comp is not given, then the standard Lua operator < is used instead.
The sort algorithm is not stable, that is, elements considered equal by the given order may have their relative positions changed by the sort.


A tutorial can be found here http://www.wowwiki.com/HOWTO:_Do_Tricks_With_Tables
on how to do tricks with tables. Example Table.sort


When you sort the table. Do not index the table. Example of what to do.

Code: Select all

array = &#123; 
&#123; stuff here &#125;,
&#123; stuff here &#125;,
&#123; stuff here &#125;,
&#123; stuff here &#125;,
&#123; stuff here &#125;,
&#125;


Post Reply