I have code that returns an NSComparisonResult. My codes works great for sorting my arrays in Ascending and Descending order when they are NSStrings with only alphabetic characters in them.
When the NSStrings have numbers in them, such as Seinfeld (Season 5) and Seinfeld (Season 6) they are sorting correctly Ascending in the order of 5 and then 6. But Descending it still sorts it as 5 and 6. Here’s an example:
Ascending:
Batman,
Seinfeld (Season 5),
Seinfeld (Season 6),
Zoolander
Descending:
Zoolander,
Seinfeld (Season 5),
Seinfeld (Season 6),
Batman
The descending code I have looks like this to return the comparison:
return [(NSString *)secondString compare:(NSString *)firstString options:NSNumericSearch];
Any ideas on how to reverse NSStrings with numbers in them?
By the way, I can’t use NSSortDescriptor in my case. The code I have requires a custom function to be built because I have to do some string appending, etc, before the sort occurs. I didn’t include it all for simplicity.
Essentially the same comment as above, but here’s full source and output to prove it works (iOS 4.3 simulator tested):
output:
In answer to Ethan Allen, here’s the output for a test with numbers in front: