iOS筆記之陣列排序

Adrenine發表於2018-01-09

簡單說明

陣列排序比較多,我們講一種比較常用的,這裡涉及到一個知識點: NSComparisonResult:

typedef enum NSComparisonResult : NSInteger {
    NSOrderedAscending = -1L,
    NSOrderedSame,
    NSOrderedDescending
} NSComparisonResult;
複製程式碼

文件解釋
NSOrderedAscending: 左邊資料比右邊小,可理解成增序 NSOrderedSame: 相等 NSOrderedDescending: 左邊資料比右邊大,可理解成降序


示例:

NSArray *array = @[@"Benz",@"BMW",@"Ferrari",@"Lamborghini"];
複製程式碼

1、普通排序系統自帶的升序

NSArray *sortedArray1 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"普通排序:%@",sortedArray1);
複製程式碼

2、逆轉陣列

NSArray *sortedArray2 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
  //逆轉
  return NSOrderedDescending;
}];
NSLog(@"逆轉陣列:%@",sortedArray2);
複製程式碼

3、不逆轉(相當於原陣列)

NSArray *sortedArray3 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
  //不逆轉
  return NSOrderedAscending;
}];
NSLog(@"不逆轉(原資料):%@",sortedArray3);
複製程式碼

4、 升序排列

NSArray *sortedArray4 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
  //升序
  NSComparisonResult result = [obj1 compare: obj2];
  return result;
}];
NSLog(@"升序排列:%@",sortedArray4);
複製程式碼

5、降序排列

NSArray *sortedArray5 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  //降序
  NSComparisonResult result = [obj1  compare: obj2];
  return -result;
}];
NSLog(@"降序排列:%@",sortedArray5);
複製程式碼

輸出結果


進階:

陣列裡面存放模型,根據模型的某個屬性值來對陣列進行重新排序

1)、初始化一些車輛和陣列:

Car *benz = [Car new];
benz.name = @"Benz";
benz.price = 2000.0;
    
Car *bmw = [Car new];
bmw.name = @"BMW";
bmw.price = 800;
    
Car *ferrari = [Car new];
ferrari.name = @"Ferrari";
ferrari.price = 1200;
    
Car *lamborghini = [Car new];
lamborghini.name = @"Lamborghini";
lamborghini.price = 12000;
    
NSMutableArray *array = @[benz,bmw,ferrari,lamborghini].mutableCopy;
複製程式碼

2)、排序方法

/**
 對陣列裡存放model的某個屬性對陣列進行重新排序

 @param array 待排序陣列
 @param increase 是否增序排列
 @param key 屬性
 @return <#return value description#>
 */
-(NSArray *)sortArray:(NSArray *)array increase:(BOOL)increase byKey:(NSString *)key{
    
    array = [array sortedArrayUsingComparator:
             ^NSComparisonResult(Car *obj1, Car *obj2) {
                 NSComparisonResult result;
                 if ([key isEqualToString:@"price"]) {
                     CGFloat res1 = [[obj1 valueForKey:key] floatValue];
                     CGFloat res2 = [[obj2 valueForKey:key] floatValue];
                     if (increase) {
                         if (res1 < res2){
                             result = NSOrderedAscending;
                         }else {
                             result = NSOrderedDescending;
                         }
                     } else {
                         if (res1 < res2){
                             result = NSOrderedDescending;
                         }else {
                             result = NSOrderedAscending;
                         }
                     }
                 } else {
                     result = [[obj1 valueForKey:key] compare:[obj2 valueForKey:key]];
                     if (!increase) {
                         if (result == NSOrderedDescending) {
                             result = NSOrderedAscending;
                         } else if (result == NSOrderedAscending) {
                             result = NSOrderedDescending;
                         }
                     }
                 }
                 
                 return result;
             }].mutableCopy;
    return array;
}

複製程式碼

3)、列印結果

NSArray *sortArray;
sortArray = [self sortArray:array increase:YES byKey:@"price"];
NSLog(@"increase by price:");
for (Car *c in sortArray) {
  NSLog(@"car:%@ -->price:%lf\n",c.name,c.price);
}

NSLog(@"------分割線------");

sortArray = [self sortArray:array increase:NO byKey:@"price"];
NSLog(@"descend by price:");
for (Car *c in sortArray) {
  NSLog(@"car:%@ -->price:%lf\n",c.name,c.price);
}

NSLog(@"------分割線------");
    
sortArray = [self sortArray:array increase:YES byKey:@"name"];
NSLog(@"increase by name:");
for (Car *c in sortArray) {
  NSLog(@"car:%@ -->price:%lf\n",c.name,c.price);
}

NSLog(@"------分割線------");
    
sortArray = [self sortArray:array increase:NO byKey:@"name"];
NSLog(@"descend by name:");
for (Car *c in sortArray) {
  NSLog(@"car:%@ -->price:%lf\n",c.name,c.price);
}

複製程式碼

輸出結果

Warnning: 可能有眼尖的看客發現了,increase by namedescend by name順序不對,升序的時候Benz不應該在BMW前面嗎?降序Benz在BMW後面嗎?注意,這裡M是大寫,而e是小寫,大寫的ASCII碼在小寫前面,所以M比e前。

聯絡方式

郵箱: xiebangyao_1994@163.com
相關賬號:

相關文章