Advertise Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

sdkIQ for iPhone
($4.99)

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Dual Matches
($0.99)

Calcuccino Programmers' Calculator
($2.99)

SDKtoday
(free)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-06-2008, 07:00 PM   #1 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 31
Default Sorting NSArray

Im having a hard time understanding how sorting of arrays are working in ObjC.

I have a simple class (Person) that have a couple of properties such as name, phoneNumber, address etc.

Then I have a array of Person's in a NSArray.

How can I easily sort the array based on names in the Person class ?

This is how I would do it in C#

Array.Sort(_myPersonArray, delegate(Result x, Result y)
{
return x.Name.CompareTo(y.Name);
});
bergetun is offline   Reply With Quote
Old 09-06-2008, 07:25 PM   #2 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 7
Default

Reference to the sorted... instance methods of NSArray should help.
iDevGuy is offline   Reply With Quote
Old 09-06-2008, 07:28 PM   #3 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
Default

I use NSSortDescriptors to sort my arrays, but there are other ways. The following code is an example.

Code:
NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
[peopleArray sortUsingDescriptors:[NSArray arrayWithObject:lastNameSorter]];
In the in the NSSortDescriptor alloc-init line, you'll have to change the initWithKey parameter from lastName to whatever the name of your last name variable is. You'll also have to have a property declared for your last name variable before you can use is as a key.
myersn024 is offline   Reply With Quote
Old 09-06-2008, 07:31 PM   #4 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

Here's how I do it in my Person class:

This line does the sort in the NSMutableArray containing Person objects:

Code:
		[array sortUsingSelector:@selector(compareFullCommaName:)];
This method is implemented in the Person class:

Code:
-(NSComparisonResult)compareFullCommaName:(MPerson*)inPerson
{
	NSString*	selfCommaName = [self textForField:kPersonFullNameComma];
	NSString*	otherPersonCommaName = [inPerson textForField:kPersonFullNameComma];
	
	return [selfCommaName compare:otherPersonCommaName];
}
The full comma names are 'lastname, firstname' but you get the idea. You need a comparison method that returns an appropriate NSComparisonResult. Look at the docs for sortUsingSelector:

BTW, answers to basic Cocoa/Foundation questions like this can be found on Cocoabuilder.com. See for instance http://www.cocoabuilder.com/search/a...rds=sort+array

Last edited by PhoneyDeveloper; 09-06-2008 at 07:33 PM.
PhoneyDeveloper is offline   Reply With Quote
Old 09-07-2008, 06:18 AM   #5 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 31
Default

Thanks for the help guys, it really helped

Jimmy
bergetun is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements
» Stats
Members: 41,861
Threads: 49,770
Posts: 213,057
Top Poster: BrianSlick (3,139)
Welcome to our newest member, melodizzzy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:01 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0