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
Sorting arrays
Moderators: Shine, Insert_witty_name
Table.sort(table, function (a, b) return a > b end)
http://www.lua.org/manual/5.0/manual.html#2.5.6
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.
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 = {
{ stuff here },
{ stuff here },
{ stuff here },
{ stuff here },
{ stuff here },
}