复杂可变数组的排序---(数组中存的是对象)---mutablearray

本文探讨如何对包含对象的可变数组(mutablearray)进行排序,主要聚焦于Objective-C中的BookItem对象排序操作,通过查看模型头文件BookItem.h和ViewController.h的关键代码来阐述实现过程。

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

最关键的代码是:

-(void)startArraySort:(NSString *)keystring isAscending:(BOOL)isAscending
{
    //self.destinationArry=[[NSMutableArray alloc]init];
    NSSortDescriptor* sortByA = [NSSortDescriptor sortDescriptorWithKey:keystring ascending:isAscending];
    //destinationArry 排序后的数组 sourceArry 源数据
    self.destinationArry=[[NSMutableArray alloc]initWithArray:[self.sourceArry sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortByA]]];
    for (BookItem *testbook in self.destinationArry) {
        NSLog(@"%@,%@,%@,%@",testbook.bookName,testbook.bookFileName,testbook.bookDescription,testbook.modifyTime);
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     self.sourceArry =[[NSMutableArray alloc]init];
    
    [self reset];
    
    [self startArraySort:@"bookName" isAscending:YES];
    
    
    
    
}



详细代码:

模型头文件

BookItem.h

#import <Foundation/Foundation.h>

@interface BookItem : NSObject


@property NSInteger booKDataID;
@property (nonatomic,strong) NSString* bookName;
@property (nonatomic,strong) NSString* bookDescription;
@property (nonatomic,strong) NSString* bookFileName;
@property NSInteger bookFileSize;
@property (nonatomic,strong) NSDate* modifyTime;
-(BookItem *)createbook:(int)bookid;
@end
BookItem.m

#import "BookItem.h"

@implementation BookItem

-(BookItem *)createbook:(int)bookid
{
//    BookItem *booktemp=[[BookItem alloc]init];
    self.booKDataID=bookid;
    self.bookName=[NSString stringWithFormat:@"book-%d",bookid];
    self.bookFileName=@"bookfile";
    self.bookDescription=@"bookDescription";
    return self;
}
@end

排序的控制器:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (nonatomic,strong) NSMutableArray *destinationArry;
@property (nonatomic,strong) NSMutableArray *sourceArry;
@end

ViewController.m

#import "ViewController.h"
#import "BookItem.h"

@interface ViewController ()

@end

@implementation ViewController


-(void)startArraySort:(NSString *)keystring isAscending:(BOOL)isAscending
{
    //self.destinationArry=[[NSMutableArray alloc]init];
    NSSortDescriptor* sortByA = [NSSortDescriptor sortDescriptorWithKey:keystring ascending:isAscending];
    //destinationArry 排序后的数组 sourceArry 源数据
    self.destinationArry=[[NSMutableArray alloc]initWithArray:[self.sourceArry sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortByA]]];
    for (BookItem *testbook in self.destinationArry) {
        NSLog(@"%@,%@,%@,%@",testbook.bookName,testbook.bookFileName,testbook.bookDescription,testbook.modifyTime);
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     self.sourceArry =[[NSMutableArray alloc]init];
    
    [self reset];
    
    [self startArraySort:@"bookName" isAscending:YES];
    
    
    
    
}

-(void)reset
{
//    BookItem *booktest=[[BookItem alloc]init];
   
    for (int i=10; i>0; i--) {
        BookItem *booktest=[[BookItem alloc]init];
        [booktest createbook:i];
        [self.sourceArry addObject:booktest];
    }
    
    for (int i=13; i<30; i++) {
        BookItem *booktest=[[BookItem alloc]init];
        [booktest createbook:i];
        [self.sourceArry addObject:booktest];
    }

    for (int i =0; i<self.sourceArry.count; i++) {
        BookItem *book = self.sourceArry[i];
          NSLog(@"%ld %@,%@,%@,%@",book.booKDataID,book.bookName,book.bookFileName,book.bookDescription,book.modifyTime);
    }

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值