Re: Alphabetizing an array
Re: Alphabetizing an array
- Subject: Re: Alphabetizing an array
- From: Robert Fischer <email@hidden>
- Date: Wed, 2 Oct 2002 09:49:14 +0200
On Dienstag, Okt 1, 2002, at 05:18 Europe/Berlin, Jan Van Tol wrote:
Can someone suggest a way to alphabetize an NSArray (or
NSMutableArray)? Alternatively, is there a way to get an NSTableView
to alphabetize what it displays? Or better yet, a way to alphabetize
the keys of an NSDictionary?
Here is a small code snippet which sorts AdressBook records by their
last name:
// sort members
NSArray *members = [[addressGroup members]
sortedArrayUsingSelector:@selector( compareLastName: )
];
You may implement the compare method in a category, so you don't have
to subclass the class of the array's elements.
//-------------------------------------------------------------------
// category of ABPerson
//-------------------------------------------------------------------
@implementation ABPerson (ALCompare)
- (NSComparisonResult)compareLastName:(ABPerson*)aPerson
{
// no checks ...
return [
(NSString*)[self valueForProperty:kABLastNameProperty] compare:
(NSString*)[aPerson valueForProperty:kABLastNameProperty]
];
}
@end
//-------------------------------------------------------------------
Cheers,
Robert
--
--- - .-. -- --
--- / \ ---- tin-b Software
.-. / \ --- .-. __o .-. (i)
/ \ / \ / \ _`\<,_ / \ Robert Fischer
/ \ / \ / \ (*)/ (*) / `---------------
/ `---' `-' `-----------'
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.