2014-01-19 81 views
-1

我有一個關於排序NSMutableArray的問題。例如我有一個NSMutableArray元素,它是NSArray。NSMutableArray排序

(
    (
    "00:07", 
    name1, 
), 
    (
    "03:07", 
    name1, 
), 
(
    "01:07", 
    name2, 
), 
) 

我需要通過NSArray的第一個元素對NSMutableArray進行排序。怎麼做?

回答

1

這是做自定義排序方式:

[myArray sortUsingComparator:^NSComparisonResult(NSArray *a, NSArray *b) { 
    if (/* check if a should be after b */) { 
    return NSOrderedAscending; 
    } else if (/* check if a should be before b */) { 
    return NSOrderedDescending; 
    } 

    // they have the same sorting weight, so position doesn't matter 
    return NSOrderedSame; 
}]; 

但在你的情況,你可以更容易,通過卸載大部分工作到的NSString:

[myArray sortUsingComparator:^NSComparisonResult(NSArray *a, NSArray *b) { 
    return [[a objectAtIndex:0] compare:[b objectAtIndex:0]]; 
}];