黑馬程序員——Objective C中數組排序幾種情況的總結——黑馬 ios 技術博客

------Java培訓、Android培訓、iOS培訓、.Net培訓、期待與您交流! -------


摘要 總結OC中數組排序3種方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors:

大體上,OC中常用的數組排序有以下幾種方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors:

1、簡單排序(sortedArrayUsingSelector:)

如果只是對字符串的排序,可以利用sortedArrayUsingSelector:方法就可以了,代碼如下

?
1
2
3
4
5
6
//簡單排序
void sortArray1(){
    NSArray *array = [NSArray arrayWithObjects:@"abc",@"456",@"123",@"789",@"ef", nil];
    NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"排序後:%@",sortedArray);
}
當然,除了利用字符串自帶的compare:方法,也可以自己寫compare:方法,進行對象的比較;如下:

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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
主函數代碼如下:
?
1
2
3
4
5
6
7
8
9
10
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);
}

2、利用block語法(sortedArrayUsingComparator:

蘋果官方提供了block語法,比較方便。其中數組排序可以用sortedArrayUsingComparator:方法,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
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);
}

3、高級排序(sortedArrayUsingDescriptors:)

如果是這樣一種情況呢?Person類裏有另外一個類的變量,比如說Person類除了name,age變量,還有一輛車Car類型,Car類裏有個name屬性。對Person對象進行排序,有這樣的要求:按照Car的name排序,如果是同一輛車,也就是Car的name相同,那麼再按照年齡進行排序,如果年齡也相同,最後按照Person的name進行排序。

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

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

?
1
2
3
4
5
6
7
8
9
10
#import "Car.h"
@implementation Car
 
+(Car *)initWithName:(NSString *)name{
    Car *car = [Car alloc] init];
    car.name = name;
    return car;
}
 
@end

然後改寫Person類,實現類Person.m代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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
主函數代碼如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void sortArray4(){
        //首先來3輛車,分別是奧迪、勞斯萊斯、寶馬
        Car *car1 = [Car initWithName:@"Audio"];
        Car *car2 = [Car initWithName:@"Rolls-Royce"];
        Car *car3 = [Car initWithName:@"BMW"];
         
        //再來5個Person,每人送輛車,分別爲car2、car1、car1、car3、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方法)


發佈了0 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章