OC-数组排序-NSSortDescriptor使用

在Object-c中,当有一个班级类MyClass,还有一个学生类Student.在班级类MyClass中通过一个可变数组NSMutableArray保存了许多的学生对象。现在有一个功能需要对学生进行按各种条件排序(按姓名升序,按学号降序,按成绩升序)。

借用上面一个简单的功能来了解一下Object-c中排序描述对象NSSortDescriptor对数组的排序的使用方法。

简单的了解一下以下几个类:Student(学生类)、MyClass(班级类)

Student:(只给接口)

 1 @interface Student : NSObject

 2 {

 3     NSString * _name;

 4     NSInteger _num;

 5     NSInteger _score;

 6 }

 7 

 8 - (id)initWithName:(NSString *)name number:(NSInteger)num score:(NSInteger)score;

 9 - (void)setName:(NSString *)name;

10 - (NSString *)name;

11 - (void)setNum:(NSInteger)num;

12 - (NSInteger)num;

13 - (void)setScore:(NSInteger)score;

14 - (NSInteger)score;

15 

16 @end

MyClass:(接口)

@interface MyClass : NSObject

{

    NSString * _className;

    NSMutableArray * _stuList;

}



- (id)init;

- (void)setClassName:(NSString *)name;

- (NSString*)className;

- (void)addStudent:(Student *)student;

- (void)addStudent:(Student *)student atIndex:(NSInteger) index;

- (void)removeStudent:(Student *)student;

- (void)removeStudentAtIndex:(NSInteger) index;

- (void)replaceStudent:(Student *)student atIndex:(NSInteger) index;

- (void)showStuList;

- (void)sortedByNumber;//按照学号升序

- (void)sortedByScore;//按照分数降序

- (void)sortedByName;//按照名字降序



- (void)sortedByNameAscByNumDescByScoreAsc;

@end

MyClass:(具体实现方法)

@implementation MyClass

- (void)sortedByNumber  //按照学号升序

{

    NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:@"_num" ascending:YES];

    NSArray *sdArray = [NSArray arrayWithObjects:sd1, nil];

    [_stuList sortUsingDescriptors:sdArray];

//    [_stuList sortedArrayUsingDescriptors:sdArray];

}

- (void)sortedByScore    //按照分数降序

{

    NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"_score" ascending:NO];

    NSArray *sdArray = [[NSArray alloc]initWithObjects:sd, nil];

    [_stuList sortUsingDescriptors:sdArray];

}

- (void)sortedByName   //按照名字降序

{

    NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"_name" ascending:NO];

    NSArray *sdArray = [[NSArray array] initWithObjects:sd, nil];

    [_stuList sortUsingDescriptors:sdArray];

}

- (void)sortedByNameAscByNumDescByScoreAsc //按照名字升序,学号降序,分数升序来排列数组中的对象

{

    NSSortDescriptor *sdName = [[NSSortDescriptor alloc]initWithKey:@"_name" ascending:YES];

    NSSortDescriptor *sdNum = [[NSSortDescriptor alloc]initWithKey:@"_num" ascending:NO];

    NSSortDescriptor *sdScore = [NSSortDescriptor sortDescriptorWithKey:@"_score" ascending:YES];

    NSArray *sdArray = [[NSArray alloc] initWithObjects:sdName,sdNum,sdScore, nil];

    [_stuList sortUsingDescriptors:sdArray];

}

@end

就由上面的 sortedByNameAscByNumDescByScoreAsc 方法来分析NSSortDescriptor对象的使用方法。

1.通过NSSortDescriptor对象的对象方法 initWithKey 或是直接通过类方法  sortDescriptorWithKey 生成一个NSSortDescriptor(排序描述)对象,该对象传进一个排序关键字(该关键字是要排序的数组中元素对象的属性),并且设置按照该关键字(属性)是按照升序还是降序。

2.将NSSortDescriptor对象(可以多个)添加到一个数组中。

3.最后通过数组的方法 sortUsingDescriptors ,将第二步中的数组作为参数传递进去,获得的结果就是已经排序好的数组了。

以上方法适用于可变数组和不可变数组,只是方法略微有点不一样。

你可能感兴趣的:(script)