如何对填充的数组进行排序[UIFont familyNames]
按字母顺序排列?
最简单的方法是提供一个排序选择器(苹果的文档 http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#//apple_ref/doc/uid/20000132-SW5详情)
Objective-C
sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Swift
let descriptor: NSSortDescriptor = NSSortDescriptor(key: "YourKey", ascending: true, selector: "localizedCaseInsensitiveCompare:")
let sortedResults: NSArray = temparray.sortedArrayUsingDescriptors([descriptor])
Apple 提供了多种用于字母排序的选择器:
-
compare:
-
caseInsensitiveCompare:
-
localizedCompare:
localizedCaseInsensitiveCompare:
localizedStandardCompare:
Swift
var students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
students.sort()
print(students)
// Prints "["Abena", "Akosua", "Kofi", "Kweku", "Peter"]"
参考 https://developer.apple.com/documentation/swift/array/1688499-sort
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)