Sorting arrays

Arrays often require sorting. Immutable arrays cannot be sorted, but mutable ones can. There are several ways to sort an NSMutableArray. The most common is using the NSMutableArray method:

 ​ ​ ​ ​-​ ​(​v​o​i​d​)​s​o​r​t​U​s​i​n​g​D​e​s​c​r​i​p​t​o​r​s​:​(​N​S​A​r​r​a​y​ ​*​)​s​o​r​t​D​e​s​c​r​i​p​t​o​r​s​;​

The argument is an array of NSSortDescriptor objects. A sort descriptor has the name of a property of the objects contained in the array and whether that property should be sorted in ascending or descending order. Imagine you had a list of doctors. If you wanted to sort the list by last name in ascending (A-Z) order, then you would create the following sort descriptor:

 ​ ​ ​ ​N​S​S​o​r​t​D​e​s​c​r​i​p​t​o​r​ ...

Get Objective-C Programming: The Big Nerd Ranch Guide 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.