DotSlashSlash DotSlashSlash - 1 year ago 114
Objective-C Question

How to sort a NSArray alphabetically?

How can I sort an array filled with

[UIFont familyNames]
into alphabetical order?

Answer Source

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:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download