Objective c NSMutableArray排序任务
我有Objective c NSMutableArray排序任务,objective-c,ios,cocoa-touch,nsmutablearray,nsarray,Objective C,Ios,Cocoa Touch,Nsmutablearray,Nsarray,我有NSMutableArray包含NSArray对象,它看起来像这样 ( "2012-10-10 13:13:29 +0000", cc5772389efc1f93bedaab872bad542a1c8432af, "{\n size = {360, 480}\n CGImage.size = {480, 360}\n imageOrientation = UIImageOrientationRight\n}" ),
NSMutableArray
包含NSArray
对象,它看起来像这样
(
"2012-10-10 13:13:29 +0000",
cc5772389efc1f93bedaab872bad542a1c8432af,
"{\n size = {360, 480}\n CGImage.size = {480, 360}\n imageOrientation = UIImageOrientationRight\n}"
),
(
"2012-10-10 13:13:37 +0000",
6c09d99f940af8e7d1d1392d28abee2133b7ac75,
"{\n size = {360, 480}\n CGImage.size = {480, 360}\n imageOrientation = UIImageOrientationRight\n}"
),
(
"2012-10-10 13:13:19 +0000",
0165776bd9e96e640c905471ddb8630c22a8911c,
"{\n size = {360, 480}\n CGImage.size = {480, 360}\n imageOrientation = UIImageOrientationRight\n}"
)
每个NSArray
对象在第一个索引处都包含NSDate
对象,我想将此NSMutableArray
从旧到新排序
有什么想法吗 使用
使用
您可以使用NSMutableArray的
sortUsingComparator:
使用给定块指定的比较方法对数组进行排序
-(void)排序比较器:(NSComparator)cmptr
参数
cmptr
A块
要颠倒顺序,只需返回
[date2 compare:date1]
即可使用NSMutableArray的
sortUsingComparator:
使用给定块指定的比较方法对数组进行排序
-(void)排序比较器:(NSComparator)cmptr
参数
cmptr
A块
要颠倒顺序,只需返回[date2比较:date1]
NSInteger dateSort(id obj1, id obj2, void *context)
{
NSData *date1 = [obj1 objectAtIndex:0] ;
NSData *date2 = [obj2 objectAtIndex:0] ;
return [date1 compare:date2];
}
NSArray *sortedArray = [array sortedArrayUsingFunction:dateSort context:NULL];
[array sortUsingComparator: ^(NSArray *array1, NSArray *array2) {
NSDate *date1 = [array1 objectAtIndex:0];
NSDate *date2 = [array2 objectAtIndex:0];
return [date1 compare:date2];
}];