小编典典

如何按字母顺序对 NSArray 进行排序?

all

如何[UIFont familyNames]按字母顺序对填充的数组进行排序?


阅读 115

收藏
2022-03-25

共1个答案

小编典典

最简单的方法是提供一个排序选择器(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"]"

参考

2022-03-25