DotSlashSlash DotSlashSlash - 4 months ago 48
Objective-C Question

How to sort a NSArray alphabetically?

How can I sort an array filled with

[UIFont familyNames]
into alphabetical order?


The simplest approach is, to provide a sort selector (Apple's documentation for details)


sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];


let descriptor: NSSortDescriptor = NSSortDescriptor(key: "YourKey", ascending: true, selector: "localizedCaseInsensitiveCompare:")
let sortedResults: NSArray = temparray.sortedArrayUsingDescriptors([descriptor])

Apple provides several selectors for alphabetic sorting:

  • compare:
  • caseInsensitiveCompare:
  • localizedCompare:
  • localizedCaseInsensitiveCompare:
  • localizedStandardCompare: