1. 程式人生 > >NSMutableArray中各種自定義排序方法

NSMutableArray中各種自定義排序方法

-(void) test2{

//宣告一個數組

    NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"123",@"23",@"45",@"67", nil];

NSMutableString *outputBefor = [[NSMutableStringalloc] init];

    for (NSString *str in sortArray) {

        [outputBefor appendFormat:@"%@ ",str];

    }

    NSLog(@"排序前:%@",outputBefor);

    NSArray *arr = [sortArray sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2){

        if ([obj1 integerValue] > [obj2 integerValue]) {

return (NSComparisonResult)NSOrderedDescending;

        }

        if ([obj1 integerValue] < [obj2 integerValue]) {

return (NSComparisonResult)

NSOrderedAscending;

        }

return (NSComparisonResult)NSOrderedSame; 

    }];

}

/*

    利用sortedArrayUsingFunction呼叫對應方法customSort

這個方法中的Obje1obj2分別是指陣列中的物件

 */

SecondMethod.m

NSInteger customSort(id obj1,id obj2){

    if ([obj1 integerValue] > [obj2 integerValue]) {

return (NSComparisonResult

)NSOrderedDescending;

    }

    if ([obj1 integerValue] < [obj2 integerValue]) {

return (NSComparisonResult)NSOrderedAscending;

    }

return (NSComparisonResult)NSOrderedSame;

}

-(void)twotest1{

    NSArray *sortArray = [[NSArray alloc] initWithObjects:@"325",@"4516",@"457",@"325", nil];

NSMutableString *outputBefor = [[NSMutableStringalloc] init];

    for (NSString *str in sortArray) {

        [outputBefor appendFormat:@"%@ ",str];

    }

    NSLog(@"排序前: %@",outputBefor);

NSArray *array = [sortArray sortedArrayUsingFunction:customSortcontext:nil];

NSMutableString *outputAfter = [[NSMutableStringalloc] init];

    for (NSString *str in array) {

        [outputAfter appendFormat:@"%@ ",str];

    }

    NSLog(@"排序後%@",outputAfter);

}


/*

    利用sortUsingDescriptors呼叫NSSortDescriptor

 我們以陣列的排序為例(也許NSSortDescriptor最常用的地方是NSFetchedResultsController中,但用法大致相同)。

 假設要對userArray陣列中的物件進行排序,而陣列中含有多個User物件(User繼承於NSManagedObject),User中有一個屬性叫做country

 [plain]

 1. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"country" ascending:YES];

 2. [userArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

這樣,就可以根據每一個Usercountry來進行升序或降序的排列,sortUsingDescriptors的引數是一個數組,也就是說可以使用多個排序引數。

 再比如說,User有一個關係叫做imageimage有個屬性叫做timestamp,如果要根據Userimage關係的timestamp排序,僅需把上面程式碼中的country改為,image.timestamp

 */

Score.h

@interface Score : NSObject

@property(nonatomic,assign)NSInteger score;

-(id)initWithScore:(NSInteger)score;

@end


Score.m

@implementation Score

-(id)initWithScore:(NSInteger)score{

self = [superself];

    if (self) {

        _score = score;

    }

return self;

}

@end

Student.h

#include "Score.h"

@interface Student : NSObject

@property (nonatomic,copy)NSString *name;

@property(nonatomic,copy)Score *stuScore;

-(id)initWithName:(NSString *)name andScore:(NSInteger)score;

@end


Student.m

@implementation Student

-(id)initWithName:(NSString *)name andScore:(NSInteger)score{

self = [superself];

    if (self) {

        _name = name;

        _stuScore = [[Score alloc] initWithScore:score];

    }

return self;

}

@end

ThirdMethod.h

@interface three : NSObject

-(void)sortedPrint;

@end

ThirdMethod.m

@implementation three

-(void)sortedPrint{

NSMutableArray *array = [[NSMutableArrayalloc] init];

    for (int i = 0; i < 9; i ++) {

        NSInteger random = arc4random() % 21;

        Student *stu = [[Student alloc] initWithName:@"luo" andScore:random];

        [array addObject:stu];

    }

    NSLog(@"%@",array);

NSSortDescriptor *descriptor = [[NSSortDescriptoralloc] initWithKey:@"stuScore.score"ascending:YES];

    [array sortUsingDescriptors:[NSArrayarrayWithObject:descriptor]];

    NSLog(@"%@",array);

}

@end