2010-08-16 53 views

回答

2

假設您的NSDate對象位於帶有@「DateKey」鍵的字典中。

NSInteger dateSort(id dict1, id dict2, void* context){ 
    return [[dict1 objectForKey:@"DateKey"] compare:[dict2 objectForKey:@"DateKey"]]; 
} 

[dateArray sortUsingFunction:dateSort context:NULL]; 

或iOS 4溶液:

[dateArray sortUsingComparator:(NSComparator)^(id obj1, id obj2){ 
    return [[obj1 objectForKey:@"DateKey"] compare:[obj1 objectForKey:@"DateKey"]]; 
    }]; 

下面的行排序應該含有的NSDate對象的NSMutableArray:

[dateArray sortUsingSelector:@selector(compare:)]; 
+0

的OP似乎排序'NSDictionary'對象的'NSMutableArray' ,每個包含一個'NSDate'。自定義比較方法是必需的。 – Justin 2010-08-16 10:30:18

+0

@Justin,謝謝,太快讀出這個問題並且錯過了它的觀點。 – Vladimir 2010-08-16 10:54:41

+0

謝謝弗拉基米爾 – 2010-08-30 08:31:12