0

私はオブジェクトを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を古いものから新しいものへと並べ替えたいと思います。

何か案が?

4

2 に答える 2

4

NSMutableArrayのを使用できます-sortUsingComparator:

sortUsingComparator:
指定されたNSComparatorブロック で指定された比較方法を使用して配列を並べ替えます。

- (void)sortUsingComparator:(NSComparator)cmptr
パラメータ コンパレータブロックcmptr

[array sortUsingComparator: ^(NSArray *array1, NSArray *array2) {
    NSDate *date1 = [array1 objectAtIndex:0];
    NSDate *date2 = [array2 objectAtIndex:0];
    return [date1 compare:date2];
}];

順序を逆にするには、単に戻ることができます[date2 compare:date1]

于 2012-10-10T13:44:49.907 に答える
0

-sortedArrayUsingFunction:contextを使用します:

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];
于 2012-10-10T13:32:06.230 に答える