Objective-C文法之NSSortDescriptor

來源:互聯網
上載者:User

標籤:

main.m

 1 #import <Foundation/Foundation.h> 2 #import "Person.h" 3 /** 4  NSSortDescriptor 可以實現按照對象的屬性進行排序;支援多個屬性排序。比如我們有個Person對象,它有名字(name)和年齡(age)兩個屬性,我們需要按Person的age屬性(降序)和name屬性(升序)來輸出Person對象數組資訊 5  */ 6 void testSortDescriptor() { 7     NSMutableArray *mArrPerson = [[NSMutableArray alloc] initWithCapacity:5]; 8     Person *p = [[Person alloc] initWithName:@"KK" age:23]; 9     [mArrPerson addObject:p];10     p = [[Person alloc] initWithName:@"Candy" age:22];11     [mArrPerson addObject:p];12     p = [[Person alloc] initWithName:@"Wiky" age:27];13     [mArrPerson addObject:p];14     p = [[Person alloc] initWithName:@"Stone" age:32];15     [mArrPerson addObject:p];16     p = [[Person alloc] initWithName:@"Tom" age:28];17     [mArrPerson addObject:p];18     p = [[Person alloc] initWithName:@"Sherlock" age:27];19     [mArrPerson addObject:p];20     p = [[Person alloc] initWithName:@"Alex" age:29];21     [mArrPerson addObject:p];22     p = [[Person alloc] initWithName:@"Keye" age:28];23     [mArrPerson addObject:p];24     25     NSLog(@"按Person的age屬性(降序)和name屬性(升序)");26     NSSortDescriptor *sortByAge = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:NO];27     NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];28     [mArrPerson sortUsingDescriptors:@[sortByAge, sortByName]];29     for (Person *p in mArrPerson) {30         NSLog(@"age=%ld, name=%@", p.age, p.name);31     }32 }33 int main(int argc, const char * argv[]) {34     @autoreleasepool {35         testSortDescriptor();36     }37     return 0;38 }

 

Person.h

1 #import <Foundation/Foundation.h>2 @interface Person : NSObject3 @property (nonatomic, strong) NSString *name;4 @property (nonatomic, assign) NSInteger age;5 6 - (id)initWithName:(NSString *)name age:(NSInteger)age;7 @end

 

Person.m

 1 #import "Person.h" 2  3 @implementation Person 4 - (id)initWithName:(NSString *)name age:(NSInteger)age { 5     if (self = [super init]) { 6         _name = name; 7         _age = age; 8     } 9     return self;10 }11 @end

 

結果:

1 2015-03-28 12:27:13.240 OCNSSortDescriptor[1276:49262] 按Person的age屬性(降序)和name屬性(升序)2 2015-03-28 12:27:13.241 OCNSSortDescriptor[1276:49262] age=32, name=Stone3 2015-03-28 12:27:13.241 OCNSSortDescriptor[1276:49262] age=29, name=Alex4 2015-03-28 12:27:13.241 OCNSSortDescriptor[1276:49262] age=28, name=Keye5 2015-03-28 12:27:13.241 OCNSSortDescriptor[1276:49262] age=28, name=Tom6 2015-03-28 12:27:13.242 OCNSSortDescriptor[1276:49262] age=27, name=Sherlock7 2015-03-28 12:27:13.242 OCNSSortDescriptor[1276:49262] age=27, name=Wiky8 2015-03-28 12:27:13.242 OCNSSortDescriptor[1276:49262] age=23, name=KK9 2015-03-28 12:27:13.242 OCNSSortDescriptor[1276:49262] age=22, name=Candy

 

Objective-C文法之NSSortDescriptor

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.