NSArray数组的排序

main.m
 
//
//  main.m
//  NSArraySort
//
//  Created by Rayln Guan on 9/4/13.
//  Copyright (c) 2013 Rayln Guan. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Student.h"

void arraySort1(){
    NSArray *arr = [NSArray arrayWithObjects:@"1",@"3",@"2",@"0", nil];
    //告诉Array用compare方法去比较 例如: [@"2" compare:@"3"]
    NSArray *newArr = [arr sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"newArr%@", newArr);
}

void arraySort2(){
    Student *stu1 = [Student student:@"guan" :@"ruilin"];
    Student *stu2 = [Student student:@"a" :@"bc"];
    Student *stu3 = [Student student:@"h" :@"ga"];
    Student *stu4 = [Student student:@"a" :@"aa"];
    NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
    NSArray *newArr = [arr sortedArrayUsingSelector:@selector(sort:)];
    NSLog(@"%@:", newArr);
}
//使用block排序
void arraySort3(){
    Student *stu1 = [Student student:@"guan" :@"ruilin"];
    Student *stu2 = [Student student:@"a" :@"bc"];
    Student *stu3 = [Student student:@"h" :@"ga"];
    Student *stu4 = [Student student:@"a" :@"aa"];
    NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];

    NSArray *newArr = [arr sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *stu) {
        NSComparisonResult result1 = [obj1.firstname compare:stu.firstname];
        if(result1 == NSOrderedSame){
            result1 = [obj1.lastname compare:stu.lastname];
        }
        return result1;
    }];
    NSLog(@"%@:", newArr);
}

//使用属性进行比较
void arraySort4(){
    Student *stu1 = [Student student:@"guan" :@"ruilin" :@"book2"];
    Student *stu2 = [Student student:@"a" :@"bc" :@"book1"];
    Student *stu3 = [Student student:@"h" :@"ga" :@"book1"];
    Student *stu4 = [Student student:@"a" :@"aa" :@"book3"];
    
    NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
    //key中的值是写@property中的值!!切记,不是属性_firstname
    NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
    NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
    NSSortDescriptor *sort3 = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
    NSArray *newArr = [arr sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sort1,sort2,sort3,nil]];
    NSLog(@"---description:%@", newArr);
}

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

    @autoreleasepool {
        arraySort1();
        arraySort2();
        arraySort3();
        arraySort4();
    }
    return 0;
}




Student.h
//
//
//  Student.h
//  NSArraySort
//
//  Created by Rayln Guan on 9/4/13.
//  Copyright (c) 2013 Rayln Guan. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Book.h"

@interface Student : NSObject

@property (nonatomic, retain) NSString *firstname;
@property (nonatomic, retain) NSString *lastname;
@property (nonatomic, retain) Book *book;

+ (Student *) student:(NSString *)firstname :(NSString *)lastname;
+ (Student *) student:(NSString *)firstname :(NSString *)lastname :(NSString *)book;
- (NSComparisonResult) sort:(Student *)stu;
@end



Student.m
//
//  Student.m
//  NSArraySort
//
//  Created by Rayln Guan on 9/4/13.
//  Copyright (c) 2013 Rayln Guan. All rights reserved.
//

#import "Student.h"

@implementation Student

+(Student *)student:(NSString *)fristname :(NSString *)lastname{
    Student *stu = [[[Student alloc] init] autorelease];
    stu.firstname = fristname;
    stu.lastname = lastname;
    return stu;
}

+(Student *)student:(NSString *)firstname :(NSString *)lastname :(NSString *)book{
    Student *stu = [[[Student alloc] init] autorelease];
    stu.firstname = firstname;
    stu.lastname = lastname;
    Book *bookAlloc = [[[Book alloc] init] autorelease];
    bookAlloc.name = book;
    stu.book = bookAlloc;
    return stu;
}

//实现排序的方法!!!
-(NSComparisonResult)sort:(Student *)stu{
    NSComparisonResult result1 = [self.firstname compare:stu.firstname];
    if(result1 == NSOrderedSame){
        result1 = [self.lastname compare:stu.lastname];
    }
    return result1;
}

-(void)dealloc{
    //NSLog(@"Student has been distory!! %@", self.firstname);
    [_firstname release];
    [_lastname release];
    [_book release];
    [super dealloc];
}
- (NSString *)description{
    return [NSString stringWithFormat:@"%@-%@--book:%@", _firstname, _lastname, _book.name];
}
@end




Book.h
//
//  Book.h
//  NSArraySort
//
//  Created by Rayln Guan on 9/5/13.
//  Copyright (c) 2013 Rayln Guan. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Book : NSObject

@property (nonatomic, retain) NSString *name;

@end



Book.m
//
//  Book.m
//  NSArraySort
//
//  Created by Rayln Guan on 9/5/13.
//  Copyright (c) 2013 Rayln Guan. All rights reserved.
//

#import "Book.h"

@implementation Book

- (void)dealloc{
    NSLog(@"Book has been destory!! %@", self);
    [super dealloc];
}

@end

猜你喜欢

转载自rayln.iteye.com/blog/1936464