NSArray 數組排序

  1. //方法1,使用自帶的比較器數組

  2. //compare是數組自帶的比較方法spa

  3. NSArray *array=[NSArray arrayWithObjects:@"3",@"1",@"2", nil];code

  4. NSArray *array2= [array sortedArrayUsingSelector:@selector(compare:)];orm

  5. NSLog(@"%@",array2);對象

結果是升序排列排序

  1. //方式二:使用塊完成排ip

  2. NSArray *array = [NSArray arrayWithObjects:@"1bc",@"4b6",@"123",@"789",@"3ef", nil];string

  3. NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {it

  4.            

  5. //這裏的代碼能夠參照上面compare:默認的排序方法,也能夠把自定義的方法寫在這裏,給對象排序io

  6.    NSComparisonResult result = [obj1 compare:obj2];

  7.    return result;

  8. }];

  9. NSLog(@"排序後:%@",sortedArray);

方法3:自定義排序

  1. #import <Foundation/Foundation.h>

  2.  

  3. @interface Person : NSObject

  4. @property NSString* name;

  5. @property int age;

  6.  

  7. -(id)initWithNameAndAge:(NSString*) aName and:(int) aAge;

  8. -(NSComparisonResult)comparePersonByAge:(Person *)person;

  9. -(NSComparisonResult)comparePersonByName:(Person *)person;

  10. @end

  1. #import "Person.h"

  2.  

  3. @implementation Person

  4. @synthesize name,age;

  5. -(id)initWithNameAndAge:(NSString*) aName and:(int) aAge{

  6.    if (self=[super init]) {

  7.        

  8.        name=aName;

  9.        age=aAge;

  10.        

  11.    }

  12.    return self;

  13. }

  14.  

  15. //自定義排序方法

  16. -(NSComparisonResult)comparePersonByAge:(Person *)person{

  17.    //默認按年齡排序

  18.    NSComparisonResult result = [[NSNumber numberWithInt:person.age] compare:[NSNumber numberWithInt:self.age]];//注意:基本數據類型要進行數據轉換

  19.    //若是年齡同樣,就按照名字排序

  20.    //if (result == NSOrderedSame) {

  21.    //    result = [self.name compare:person.name];

  22.    //}

  23.    return result;

  24. }

  25.  

  26. -(NSComparisonResult)comparePersonByName:(Person *)person{

  27.    //默認按年齡排序

  28.    NSComparisonResult result = [ person.name compare:self.name];//注意:基本數據類型要進行數據轉換

  29.    //若是年齡同樣,就按照名字排序

  30.    if (result == NSOrderedSame) {

  31.        result = [[NSNumber numberWithInt:person.age] compare:[NSNumber numberWithInt:self.age]];

  32.    }

  33.    return result;

  34. }

  35.  

  36.  

  37. - (NSString *)description

  38. {

  39.    return [NSString stringWithFormat:@"%@    %d", name,age];

  40. }

  41. @end

  1. #import <Foundation/Foundation.h>

  2. #import "Person.h"

  3.  

  4. int main(int argc, const char * argv[]) {

  5.    @autoreleasepool {

  6.        Person *p1 = [[Person alloc]initWithNameAndAge:@" qweasadsasd" and:25];

  7.        Person *p2 = [[Person alloc]initWithNameAndAge:@"\t1234" and:28];

  8.        Person *p3 = [[Person alloc]initWithNameAndAge:@"123" and:2];

  9.        Person *p4 = [[Person alloc]initWithNameAndAge:@"zxc" and:89];

  10.        Person *p5 = [[Person alloc]initWithNameAndAge:@"123" and:8];

  11.        

  12.        NSArray * persons = [NSArray arrayWithObjects:p1,p2,p3,p4,p5,nil];

  13.        NSArray *sortedArray = [persons sortedArrayUsingSelector:@selector(comparePersonByName:)];

  14.        NSLog(@"排序後:%@",sortedArray);

  15.    }

  16.    return 0;

  17. }

方法四:高級排序

相關文章
相關標籤/搜索