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 ...
Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.