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

推荐答案

最简单的方法是,提供一个排序 Select 器(详细信息为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为字母排序提供了几个 Select 器:

  • compare:
  • 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相关问答推荐

日期更改时的 UIDatePicker

如何获取 NSDictionary 中的第一个元素?

从 float 或 double 实例化 NSDecimalNumber 的正确方法

在 iOS 中检测 PAN 手势的方向

Xcode 中架构的重复符号

我怎样才能拥有一个同时包含图像和文本的 UIBarButtonItem?

iOS框架改变一个属性(例如宽度)

使用 Objective-C 查找 IMEI 号码

在 NSString 对象中查找子字符串

在 Objective C 中使用 extern

强制iphone应用程序以编程方式重新启动?

如何以编程方式移动 UIScrollView 以集中在键盘上方的控件中?

关闭一次后如何返回允许推送通知对话框?

使用 NSLog 打印 NSData

Modal segue,导航栏消失

NS_ASSUME_NONNULL_BEGIN 宏

如何在 Objective-C 中添加具有自己的 UIViewController 的子视图?

笔尖中的原型单元而不是故事板

Xcode 在调试时判断表达式

iOS 上的电话号码格式