Quote:
Originally Posted by slickfive
Hi All,
Currently I have a list of keys within an array:
A example plist is:
I have a segment control which I would like to sort A-Z or Z-A the array and the strings to display the results onto a UITableView. Keys being the headers and strings being displayed within the table.
I have this so far:
I'm not sure what I should be using for initWithKey for the above example.
Any help would be much appreciated
Thank you in advance
|
Using sortUsingSelector would be simpler:
Code:
NSArray* sortedArray = [keyArray sortedArrayUsingSelector: @selector(compare:)];
By the way, the NSDictionary method allKeys returns an immutable array. You can't sort immutable arrays "in place." You have to use one of the methods that returns a different, sorted array as output.
There are a whole bunch of different types of sort methods for arrays and mutable arrays. sortUsingSelector, sortUsingFunction, sortUsingComparator, and of course, sortUsingDescriptors.
Unless I'm sorting an array using multiple keys (e.g. an array of name objects by last name, then by first name within last name, then by age) or working with Core Data, sortUsingDescriptors is my last choice. To use it I have to create several helper objects first, and I always have to dig through the documentation and scratch my head to figure out how to create a sort descriptor that does what I want.