iOS数组排序

数组包含数字

//1
NSArray *indexArray = @[@12, @65, @3, @25, @589, @365 ,@134, @34];
//        NSSortDescriptor *idnexSort = [[NSSortDescriptor alloc] initWithKey:@"" ascending:YES];
NSArray *indexSortArray = [indexArray sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@", indexSortArray);//正序
//-> @[3,12,25,34,65,134,365,589]
NSLog(@"%@", [[indexSortArray reverseObjectEnumerator] allObjects]);//倒序
//-> @[589,365,134,65,34,25,12,3]

使用s�ortedArrayUsingSelector进行排序时, compare:只是系统提供的一个方法,可以提供自定义的对比方法

数组包含字典

//2
NSArray *dicNameArray = @[@"honzon 3",@"honzon 0",@"honzon 5",@"honzon 1",@"honzon 4",@"honzon 2"];
NSArray *dicNumArray = @[@3,@0,@5,@1,@4,@2];

NSMutableArray *dicArray = [NSMutableArray new];
for (NSString *name in dicNameArray) {
    NSNumber *num = [dicNumArray objectAtIndex:[dicNameArray indexOfObject:name]];
    [dicArray addObject:[NSDictionary dictionaryWithObject:num forKey:name]];
}

[dicArray sortUsingComparator:^NSComparisonResult(NSDictionary*  _Nonnull obj1, NSDictionary*  _Nonnull obj2) {
           
   NSUInteger number1 = [[[obj1 allValues] firstObject] integerValue];
   NSUInteger number2 = [[[obj2 allValues] firstObject] integerValue];
            
   return  number1 > number2;
}];
        
NSLog(@"%@", dicArray);
//->@[@{@"honzon 0" : @0}, @{@"honzon 1" : @1}, @{@"honzon 2" : @2}, @{@"honzon 3" : @3}, @{@"honzon 4" : @4}, @{@"honzon 5" : @5} ]

数组包含自定义对象

自定义类

@interface Honzon : NSObject
@property (nonatomic, copy)NSString *name;
@property (nonatomic, assign)NSUInteger age;
@property (nonatomic, assign, readonly)NSUInteger randomNum;
@end
@implementation Honzon

- (instancetype)init {
    self = [super init];
    if (self) {
       _randomNum = arc4random() % 3;
    }
    return self;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"name: %@, age: %tu, randomNum: %tu" ,self.name, self.age, self.randomNum];
}

@end
NSMutableArray *honzonArray = [NSMutableArray new];
for (NSString *name in dicNameArray) {
    Honzon *honzon = [Honzon new];
    NSString *age = [dicNumArray objectAtIndex:[dicNameArray indexOfObject:name]];
    
    honzon.name = name;
    honzon.age = age.integerValue;
    
    [honzonArray addObject:honzon];
}

NSSortDescriptor *ageSort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
NSSortDescriptor *randomSort = [[NSSortDescriptor alloc] initWithKey:@"randomNum" ascending:YES];

NSArray *sortAgeArray = [honzonArray sortedArrayUsingDescriptors:@[ageSort]];
NSLog(@"%@", sortAgeArray);
/*->@[
 name: honzon 0, age: 0, randomNum: 0,
 name: honzon 1, age: 1, randomNum: 2,
 name: honzon 2, age: 2, randomNum: 2,
 name: honzon 3, age: 3, randomNum: 2,
 name: honzon 4, age: 4, randomNum: 1,
 name: honzon 5, age: 5, randomNum: 1
]*/

NSArray *randomSortArray = [honzonArray sortedArrayUsingDescriptors:@[randomSort]];
NSLog(@"%@", randomSortArray);
/*->@[
 name: honzon 0, age: 0, randomNum: 0,
 name: honzon 5, age: 5, randomNum: 1,
 name: honzon 4, age: 4, randomNum: 1,
 name: honzon 3, age: 3, randomNum: 2,
 name: honzon 1, age: 1, randomNum: 2,
 name: honzon 2, age: 2, randomNum: 2
]*/
        
NSArray *ranSortAgeArray = [sortAgeArray sortedArrayUsingDescriptors:@[randomSort]];
NSLog(@"%@", ranSortAgeArray);
/*->@[
 name: honzon 0, age: 0, randomNum: 0,
 name: honzon 4, age: 4, randomNum: 1,
 name: honzon 5, age: 5, randomNum: 1,
 name: honzon 1, age: 1, randomNum: 2,
 name: honzon 2, age: 2, randomNum: 2,
 name: honzon 3, age: 3, randomNum: 2
]*/
        
NSArray *sortHonzonArray = [honzonArray sortedArrayUsingDescriptors:@[ageSort , randomSort]];
NSLog(@"%@", sortHonzonArray);
/*->@[
 name: honzon 0, age: 0, randomNum: 0,
 name: honzon 1, age: 1, randomNum: 2,
 name: honzon 2, age: 2, randomNum: 2,
 name: honzon 3, age: 3, randomNum: 2,
 name: honzon 4, age: 4, randomNum: 1,
 name: honzon 5, age: 5, randomNum: 1
]*/

自定义类,最好不要使用sortedArrayUsingDescriptors:@[ageSort , randomSort]这种方式

借花献佛:如果你自己的类需要定义排序顺序,请按照惯例合理设置sortDescriptors参数。因为,在现实中,你应该以商业逻辑来思考它,而不是用数学公式或者映射化简函数来考量它

未经本人同意,禁止转载

你可能感兴趣的:(iOS数组排序)