您可以使用Swift的内置排序函数,也可以使用Swift数组,因为Swift数组是桥接的,因此
NSArray可以
sortedArrayUsingComparator直接从swift
调用。
使用Swift的
sorted功能:
var sortedArray = sorted(persons) { (obj1, obj2) in // The downcast to Person is only needed if persons is an NSArray or a Swift Array of AnyObjects let p1 = obj1 as Person let p2 = obj2 as Person return p1.name < p2.name}
或者,使用
NSArray的
sortedArrayUsingComparator:
var sortedArray = persons.sortedArrayUsingComparator { (obj1, obj2) -> NSComparisonResult in let p1 = obj1 as Person let p2 = obj2 as Person let result = p1.name.compare(p2.name) return result}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)