数组排序

2017-03-07  本文已影响21人  ibiaoma

NSArray *array = @[@"1",@"21",@"12",@"11",@"0",@"14",@"23",@"9"];

   //升序
NSArray *sheng = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
  
    if ([obj1 integerValue] > [obj2 integerValue]) {
        return NSOrderedDescending;
    }else if ([obj1 integerValue] > [obj2 integerValue]){
        return NSOrderedAscending;
    }else{
        return NSOrderedSame;
    }
}];
NSLog(@"sheng=%@",sheng);

//降序
NSArray *jiang = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {

    if ([obj2 integerValue] > [obj1 integerValue]) {
        return NSOrderedDescending;
    }else if ([obj2 integerValue] > [obj1 integerValue]){
        return NSOrderedAscending;
    }else{
        return NSOrderedSame;
    }
}];
NSLog(@"jiang=%@",jiang);
上一篇下一篇

猜你喜欢

热点阅读