O'Reilly logo

Programming in Objective-C, Sixth Edition by Stephen G. Kochan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sorting Arrays

If your address book contains a lot of entries, alphabetizing it might be convenient. You can easily do this by adding a sort method to your AddressBook class and by taking advantage of an NSMutableArray method called sortUsingSelector:. This method takes as its argument a selector that the sortUsingSelector: method uses to compare two elements. Arrays can contain any type of objects in them, so the only way to implement a generic sorting method is to have you decide whether elements in the array are in order. To do this, you must add a method to compare two elements in the array.4 The result returned from that method is to be of type NSComparisonResult. It should return NSOrderedAscending if you want the sorting method to place ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required