如何按字母顺序对NSArray进行排序?如何对填充了[UIFont familyNames]按字母顺序?
3 回答
Qyouu
TA贡献1786条经验 获得超11个赞
目标-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贡献2065条经验 获得超14个赞
@selector(localizedCaseInsensitiveCompare:)
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; sortedArray=[anArray sortedArrayUsingDescriptors:@[sort]];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
慕哥9229398
TA贡献1877条经验 获得超6个赞
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 关注
- 1045 浏览
添加回答
举报
0/150
提交
取消