• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Alphabetizing an array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Prev by Date: Palettes that float above someone else's application
  • Next by Date: Re: Bochs - Free PC emulator
  • Previous by thread: Palettes that float above someone else's application
  • Next by thread: Threads and unintended message forwarding
  • Index(es):
    • Date
    • Thread