最关键的代码是:
-(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