forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sorting arrays

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
danabnormal



Joined: 09 May 2006
Posts: 1

PostPosted: Tue May 09, 2006 4:04 am    Post subject: Sorting arrays Reply with quote

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
Back to top
View user's profile Send private message
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Tue May 09, 2006 7:47 am    Post subject: Reply with quote

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


http://www.lua.org/manual/5.0/manual.html#2.5.6
Quote:
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[i]) 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:

array = {
{ stuff here },
{ stuff here },
{ stuff here },
{ stuff here },
{ stuff here },
}


Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group