这个是回答这篇文章的问题
http://blog.csdn.net/sinat_30162391/article/details/50916219#comments
创建个date模型
#import <Foundation/Foundation.h>
@interface Date : NSObject
@property (nonatomic, copy) NSString *time;
@end
之后赋值进行排序
Date *d1 = [[Date alloc] init];
d1.time = @"2012-12-02";
Date *d2 = [[Date alloc] init];
d2.time = @"2412-12-02";
Date *d3 = [[Date alloc] init];
d3.time = @"2312-12-02";
Date *d4 = [[Date alloc] init];
d4.time = @"2312-12-02";
Date *d5 = [[Date alloc] init];
d5.time = @"2812-08-02";
Date *d6 = [[Date alloc] init];
d6.time = @"0000-09-02";
_dataArray = [NSMutableArray array];
[_dataArray addObject:d1];
[_dataArray addObject:d2];
[_dataArray addObject:d3];
[_dataArray addObject:d4];
[_dataArray addObject:d5];
[_dataArray addObject:d6];
for (int i = 0; i < _dataArray.count; i ++) {
Date *d = [[Date alloc] init];
d = _dataArray[i];
NSLog(@"排序前 == %@", d.time);
}
NSLog(@"\n");
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"time" ascending:YES];
NSMutableArray *sortDescriptors = [[NSMutableArray alloc] initWithObjects:&sorter count:1];
NSArray *sortArray = [_dataArray sortedArrayUsingDescriptors:sortDescriptors];
for (int j = 0; j < sortArray.count; j ++) {
Date *d = [[Date alloc] init];
d = sortArray[j];
NSLog(@"排序后 == %@", d.time);
}
2017-06-10 19:59:35.046 TimeSort[4740:678220] 排序前 == 2012-12-02
2017-06-10 19:59:35.047 TimeSort[4740:678220] 排序前 == 2412-12-02
2017-06-10 19:59:35.047 TimeSort[4740:678220] 排序前 == 2312-12-02
2017-06-10 19:59:35.047 TimeSort[4740:678220] 排序前 == 2312-12-02
2017-06-10 19:59:35.047 TimeSort[4740:678220] 排序前 == 2812-08-02
2017-06-10 19:59:35.047 TimeSort[4740:678220] 排序前 == 0000-09-02
2017-06-10 19:59:35.048 TimeSort[4740:678220]
2017-06-10 19:59:35.048 TimeSort[4740:678220] 排序后 == 0000-09-02
2017-06-10 19:59:35.048 TimeSort[4740:678220] 排序后 == 2012-12-02
2017-06-10 19:59:35.048 TimeSort[4740:678220] 排序后 == 2312-12-02
2017-06-10 19:59:35.048 TimeSort[4740:678220] 排序后 == 2312-12-02
2017-06-10 19:59:35.048 TimeSort[4740:678220] 排序后 == 2412-12-02
2017-06-10 19:59:35.049 TimeSort[4740:678220] 排序后 == 2812-08-02
作者:sinat_30162391 发表于2017/6/10 20:04:15 原文链接
阅读:72 评论:0 查看评论