NSMutableArray中各种自定义排序方法

本文详细介绍了NSMutableArray的三种自定义排序方法:sortedArrayUsingComparator、sortedArrayUsingFunction和sortUsingDescriptors。通过示例代码展示了如何根据整数值、自定义比较函数以及对象属性进行排序操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NSMutableArray中各种自定义排序方法

/*

 (1)sortedArrayUsingComparator调用NScomparator方法

 */

FirstMethod.m

-(void) test2{

    //声明一个数组

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

    

    NSMutableString *outputBefor = [[NSMutableString alloc] 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 = [[NSMutableString alloc] init];

    for (NSString *str in sortArray) {

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

    }

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

    

    NSArray *array = [sortArray sortedArrayUsingFunction:customSort context:nil];

    

    NSMutableString *outputAfter = [[NSMutableString alloc] 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 = [super self];

    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 = [super self];

    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 = [[NSMutableArray alloc] 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 = [[NSSortDescriptor alloc] initWithKey:@"stuScore.score" ascending:YES];

    [array sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];

    NSLog(@"%@",array);

}

@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值