如何[UIFont familyNames]按字母顺序对填充的数组进行排序?
[UIFont familyNames]
最简单的方法是提供一个排序选择器(Apple’s documentation for details)
Objective-C
sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
迅速
let descriptor: NSSortDescriptor = NSSortDescriptor(key: "YourKey", ascending: true, selector: "localizedCaseInsensitiveCompare:") let sortedResults: NSArray = temparray.sortedArrayUsingDescriptors([descriptor])
Apple 提供了几个用于字母排序的选择器:
compare:
caseInsensitiveCompare:
localizedCompare:
localizedCaseInsensitiveCompare:
localizedStandardCompare:
var students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"] students.sort() print(students) // Prints "["Abena", "Akosua", "Kofi", "Kweku", "Peter"]"
参考