iOS 排序方法

来源:互联网 发布:js object 增加属性 编辑:程序博客网 时间:2024/05/01 09:38

一般分为三种:

第一种 使用sortedArrayUsingSelector排序

    SEL sel = @selector(compare:);
    arr = [arr sortedArrayUsingSelector:sel];


第二种 使用 sortedArrayUsingDescriptors排序

NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"FriendName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sorter count:1]; 

 [self.myArray sortedArrayUsingDescriptors:sortDescriptors]


第二种 使用 sortedArrayUsingComparator排序

NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) { if ([obj1 integerValue] > [obj2 integerValue]) {  return (NSComparisonResult)NSOrderedDescending; } if ([obj1 integerValue] < [obj2 integerValue]) {  return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame;}];

0 0
原创粉丝点击