如何按字母顺序对NSArray进行排序?如何对填充了[UIFont familyNames]按字母顺序?
3 回答

沧海一幻觉
TA贡献1824条经验 获得超5个赞
目标-C
sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
斯威夫特
let descriptor: NSSortDescriptor = NSSortDescriptor(key: "YourKey", ascending: true, selector: "localizedCaseInsensitiveCompare:") let sortedResults: NSArray = temparray.sortedArrayUsingDescriptors([descriptor])
compare:
caseInsensitiveCompare:
localizedCompare:
localizedCaseInsensitiveCompare:
localizedStandardCompare:
斯威夫特
var students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]students.sort()print(students)// Prints "["Abena", "Akosua", "Kofi", "Kweku", "Peter"]"

陪伴而非守候
TA贡献1757条经验 获得超8个赞
@selector(localizedCaseInsensitiveCompare:)
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; sortedArray=[anArray sortedArrayUsingDescriptors:@[sort]];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];

慕容3067478
TA贡献1773条经验 获得超3个赞
NSArray *sortedArrayOfString = [arrayOfString sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch]; }];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) { return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch]; }];NSArray *sortedArray = [anArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
- 3 回答
- 0 关注
- 414 浏览
添加回答
举报
0/150
提交
取消