gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:12:40 25 4
gpt4 key购买 nike

如何将填充有 [UIFont familyNames] 的数组按字母顺序排序?

最佳答案

最简单的方法是,提供一个排序选择器(Apple's documentation 了解详情)

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 提供了几个用于字母排序的选择器:

  • 比较:
  • caseInsensitiveCompare:
  • localizedCompare:
  • localizedCaseInsensitiveCompare:
  • localizedStandardCompare:

swift

var students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
students.sort()
print(students)
// Prints "["Abena", "Akosua", "Kofi", "Kweku", "Peter"]"

Reference

关于objective-c - 如何按字母顺序对 NSArray 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1351182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com