Ranking an NSMutableArray

Hi All,

Total Noob here trying to learn asoc.

Here is the situation - i have an NSMutableArray which is initialised with individual records of names. Each recored has several other ‘keys’, such as a score key eg. it is in the form of {name:theName, score, theScore, etc}

One by one i update theScore values of the appropriate name as the scores come in.

All that works well.

What i’d like to do is be able to rank the names in the array by their ‘theScore’. Is there an easy way of calculating the rank of something within an NSMutableArray?

In an ideal world I’d create a new NSMutableArray which contains the same data but ranked by the numerical value of theScore. Clearly it has to update everytime there is a new score added to the master database.

Can anyone help me (i’ve been struggling with the coco objc documentation and i kind of feel that there is a way of sorting by properties - but not understanding the syntax of objc i’m really struggling!)

Any help greatly appreciated.


You can use sortedArrayUsingDescriptors:. You need to make a suitable NSSortDescriptor first (or several), something like this:

set aSortDesc to current application'sNSSortDescripton's sortDescriptorWithKey_ascending_selector_("score", true, "localizedCompare:") 
set theList to yourArray's sortedArrayUsingDescriptors_({sortDescs})

Will try when i get home, but it seems to make sense from the office away from my script.

Will let you know how i get on, but thank you so much in advance.

I love people who know what they are doing!


One quick question does spring to mind though…

From those lines - what will “theList” actually be? will it be another nsmutablearray?
Will i have do to that set theList to NSMutableArray’s alloc()'s init() business for me to use it in a tableview, (not that i know for a moment what that does) -

It’s like i’m trying to converse in Chinese with only a takeaway menu as a dictionary!

Good point – it will return an NSArray. Better to use sortUsingDescriptors_ and modify the NSMutableArray in place.

Trying your first method after a bit of faffing i get this error:

[First_Scoreboard_attemptAppDelegate updateCountry:]: -[NSSortDescriptor count]: unrecognized selector sent to instance 0x2002710c0 (error -10000)

No idea what that means…

Any help?

PS this is the code i ended up using:

set aSortDesc to current application’s class “NSSortDescriptor”'s sortDescriptorWithKey_ascending_selector_(“frontTotalValue”, true, “localizedCompare:”)

set frontRankedArray to theCountryArray’s sortedArrayUsingDescriptors_(aSortDesc)

Cheers Oli

I suspect it means you probably left out the {}. The sort commands take a list of descriptors, hence ({sortDesc}) rather than (sortDesc). Or else you copied and pasted my typo (sortDescs isntead of sortDesc).

Cheers for that…

Ended up using the rather longwinded:

set frontDescriptor to current application’s class “NSSortDescriptor”'s alloc()'s initWithKey_ascending_(“frontTotalValue”, false, “autorelease”)
set frontSecDescriptor to current application’s class “NSSortDescriptor”'s alloc()'s initWithKey_ascending_(“frontRatio”, false, “autorelease”)
set fsortDescriptors to current application’s class “NSMutableArray”'s arrayWithObject_(frontDescriptor, frontSecDescriptor)

set frontRankedArray to theCountryArray’s sortedArrayUsingDescriptors_(fsortDescriptors)

which seems to work

Cheers for the help