美文网首页iOS DeveloperIOSiOS学习笔记
NSSort​Descriptor 数组排序

NSSort​Descriptor 数组排序

作者: boundlessocean | 来源:发表于2017-05-23 16:59 被阅读144次
u=1319301938,1311788076&fm=23&gp=0.jpg

当开发应用时,你只需要假设排序是快速的,而它的功用的衡量标准是你完成所需要任务的容易程度。从这个角度考虑,Foundation的NSSortDescriptor大概是你能找到的最有用,最优雅的实现了。

NSSortDescriptor由下述参数组成 :

  • sortDescriptorWithKey: ascending: selector:
  • key:对于一个给定的集合,对应值的键位将对集合中的每个对象进行排序。
  • ascending:指定一个集合是否按照升序(YES)还是降序(NO)进行排序的布尔值。
  • selector: 另外NSSortDescriptor还有一个涉及到排序的值之间的比较的第三个可选参数。默认情况下,这是一个简单的相等性检查,但它的行为可以通过传递一个选择器(SEL)或者比较器(NSComparator)而发生改变。

任何时候当你在为面向用户的字符串排序时,一定要加入localizedStandardCompare:选择器,它将根据当前语言环境的语言规则进行排序(语言环境可能会根据大小写,变音符号等等的顺序而发生改变)。

以下是几种使用NSSortDescriptor的不同组合来将它们排序的方法:

@interface Person : NSObject
@property NSString *firstName;
@property NSString *lastName;
@property NSNumber *age;
@end

@implementation Person

- (NSString *)description {
    return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
}

@end
NSArray *firstNames = @[ @"Alice", @"Bob", @"Charlie", @"Quentin" ];
NSArray *lastNames = @[ @"Smith", @"Jones", @"Smith", @"Alberts" ];
NSArray *ages = @[ @24, @27, @33, @31 ];

NSMutableArray *people = [NSMutableArray array];
[firstNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    Person *person = [[Person alloc] init];
    person.firstName = [firstNames objectAtIndex:idx];
    person.lastName = [lastNames objectAtIndex:idx];
    person.age = [ages objectAtIndex:idx];
    [people addObject:person];
}];

NSSortDescriptor *firstNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES selector:@selector(localizedStandardCompare:)];
NSSortDescriptor *lastNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES selector:@selector(localizedStandardCompare:)];
NSSortDescriptor *ageSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];

NSLog(@"By age: %@", [people sortedArrayUsingDescriptors:@[ageSortDescriptor]]);
// "Charlie Smith", "Quentin Alberts", "Bob Jones", "Alice Smith"


NSLog(@"By first name: %@", [people sortedArrayUsingDescriptors:@[firstNameSortDescriptor]]);
// "Alice Smith", "Bob Jones", "Charlie Smith", "Quentin Alberts"


NSLog(@"By last name, first name: %@", [people sortedArrayUsingDescriptors:@[lastNameSortDescriptor, firstNameSortDescriptor]]);
// "Quentin Alberts", "Bob Jones", "Alice Smith", "Charlie Smith"

相关文章

  • NSSort​Descriptor 数组排序

    当开发应用时,你只需要假设排序是快速的,而它的功用的衡量标准是你完成所需要任务的容易程度。从这个角度考虑,Foun...

  • iOS - NSSort​Descriptor

    排序是计算机科学中不能避免的问题,很幸运看到了一篇关于排序的NSSort​Descriptor类的文档。文档地址。...

  • iOS 奇技淫巧总结

    数组去重 利用 NSSet 的特性,NSSet 是无序、没有重复元素的数组,如果需要排序,可以考虑用 NSSort...

  • iOS 各种排序

    数组排序 数组中字典排序 数组中字典按照某个value排序 排序方法

  • Java 数组的排序、逆序

    数组的排序、逆序测试数据 数组选择排序 数组冒泡排序 数组逆序

  • java 数组和list排序

    数组排序 其中有数组排序和数组对象排序 数组一些数字排序则直接用Arrays.sort()加数组就可以。数组对象则...

  • 数组

    数组的遍历 数组是值类型 数组的排序 冒泡排序 多维数组

  • 2018-01-14

    php数组排序 sort() - 以升序对数组排序 rsort() - 以降序对数组排序 asort() - 根据...

  • PHP排序算法

    排序算法 冒泡排序(数组排序) 快速排序(数组排序) 参考 http://www.cnblogs.com/enia...

  • 算法记录

    快速排序 基本算法: 归并排序讲数组分为两个子数组分别排序,并将有序的子数组归并使得整个数组排序; 快速排序通过一...

网友评论

    本文标题:NSSort​Descriptor 数组排序

    本文链接:https://www.haomeiwen.com/subject/tflgxxtx.html