ios 數組排序

大致上,OC中經常使用的數組排序有如下幾種方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors:。


一、簡單排序(sortedArrayUsingSelector:)
若是隻是對字符串的排序,能夠利用sortedArrayUsingSelector:方法就能夠了,代碼以下

//簡單排序
void sortArray1(){
    NSArray *array = [NSArray arrayWithObjects:@"abc",@"456",@"123",@"789",@"ef", nil];
    NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"排序後:%@",sortedArray);
}
固然,除了利用字符串自帶的compare:方法,也能夠本身寫compare:方法,進行對象的比較;以下:

首先是新建了Person類,實現方法以下(頭文件就省了):

#import "Person.h"
@implementation Person

//直接實現靜態方法,獲取帶有name和age的Person對象
+(Person *)personWithAge:(int) age withName:(NSString *)name{
    Person *person = [[Person alloc] init];
    person.age = age;
    person.name = name;
    return person;
}

//自定義排序方法
-(NSComparisonResult)comparePerson:(Person *)person{
  //默認按年齡排序
    NSComparisonResult result = [[NSNumber numberWithInt:person.age] compare:[NSNumber numberWithInt:self.age]];//注意:基本數據類型要進行數據轉換
  //若是年齡同樣,就按照名字排序
    if (result == NSOrderedSame) {
        result = [self.name compare:person.name];
    }
    return result;
}

@end
主函數代碼以下:

void sortArray2(){
    Person *p1 = [Person personWithAge:23 withName:@"zhangsan"];
    Person *p2 = [Person personWithAge:21 withName:@"lisi"];
    Person *p3 = [Person personWithAge:24 withName:@"wangwu"];
    Person *p4 = [Person personWithAge:24 withName:@"liwu"];
    Person *p5 = [Person personWithAge:20 withName:@"liwu"];
    NSArray *array = [NSArray arrayWithObjects:p1,p2,p3,p4,p5, nil];
    NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(comparePerson:)];
    NSLog(@"排序後:%@",sortedArray);
}

二、利用block語法(sortedArrayUsingComparator:)
蘋果官方提供了block語法,比較方便。其中數組排序能夠用sortedArrayUsingComparator:方法,代碼以下:

void sortArray3(){
    NSArray *array = [NSArray arrayWithObjects:@"1bc",@"4b6",@"123",@"789",@"3ef", nil];
    NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

   //這裏的代碼能夠參照上面compare:默認的排序方法,也能夠把自定義的方法寫在這裏,給對象排序
        NSComparisonResult result = [obj1 compare:obj2];
        return result;
    }];
    NSLog(@"排序後:%@",sortedArray);
}

三、高級排序(sortedArrayUsingDescriptors:)
若是是這樣一種狀況呢?Person類裏有另一個類的變量,好比說Person類除了name,age變量,還有一輛車Car類型,Car類裏有個name屬性。對Person對象進行排序,有這樣的要求:按照Car的name排序,若是是同一輛車,也就是Car的name相同,那麼再按照年齡進行排序,若是年齡也相同,最後按照Person的name進行排序。

上面這樣就要使用第三種方法,利用排序描述器,很少說,有興趣能夠看看API介紹。代碼以下:

首先寫個Car類,實現類Car.m代碼以下:

#import "Car.h"
@implementation Car

+(Car *)initWithName:(NSString *)name{
    Car *car = [Car alloc] init];
    car.name = name;
    return car;
}

@end
而後改寫Person類,實現類Person.m代碼以下:

#import "Person.h"
#import "Car.h"
@implementation Person

+(Person *)personWithAge:(int)age withName:(NSString *)name withCar:(Car *)car{
    Person *person = [[Person alloc] init];
    person.age = age;
    person.name = name;
    person.car = car;
    return person;
}

//這裏重寫description方法,用於最後測試排序結果顯示
-(NSString *)description{
    return [NSString stringWithFormat:@"age is %zi , name is %@, car is %@",_age,_name,_car.name];
}

@end
主函數代碼以下:

void sortArray4(){
        //首先來3輛車,分別是奧迪、勞斯萊斯、寶馬
        Car *car1 = [Car initWithName:@"Audio"];
        Car *car2 = [Car initWithName:@"Rolls-Royce"];
        Car *car3 = [Car initWithName:@"BMW"];
        
        //再來5個Person,每人送輛車,分別爲car二、car一、car一、car三、car2
        Person *p1 = [Person personWithAge:23 withName:@"zhangsan" withCar:car2];
        Person *p2 = [Person personWithAge:21 withName:@"zhangsan" withCar:car1];
        Person *p3 = [Person personWithAge:24 withName:@"lisi" withCar:car1];
        Person *p4 = [Person personWithAge:23 withName:@"wangwu" withCar:car3];
        Person *p5 = [Person personWithAge:23 withName:@"wangwu" withCar:car2];

    
        //加入數組
        NSArray *array = [NSArray arrayWithObjects:p1,p2,p3,p4,p5, nil];
        
        //構建排序描述器
        NSSortDescriptor *carNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"car.name" ascending:YES];
        NSSortDescriptor *personNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
        NSSortDescriptor *personAgeDesc = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
        
        //把排序描述器放進數組裏,放入的順序就是你想要排序的順序
        //我這裏是:首先按照年齡排序,而後是車的名字,最後是按照人的名字
        NSArray *descriptorArray = [NSArray arrayWithObjects:personAgeDesc,carNameDesc,personNameDesc, nil];
        
        NSArray *sortedArray = [array sortedArrayUsingDescriptors: descriptorArray];
        NSLog(@"%@",sortedArray);
}
結果以下:



從結果看出,先按照age排序,若是age相同,按照car排序,若是car相同,按照name排序。

(注意:上面兩種排序方法要想實現字符串顯示,請重寫description方法)
相關文章
相關標籤/搜索