2012-08-05 102 views

回答

18
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"thisIsTheNameOfYourDateProperty" ascending:NO]; 
NSArray *orderedArray = [arrayOfCustomObjects sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
8

試試這個:

NSArray* newArray = [array sortedArrayUsingComparator: ^NSComparisonResult(MyClass *c1, MyClass *c2) 
{ 
    NSDate *d1 = c1.date; 
    NSDate *d2 = c2.date; 

    return [d1 compare:d2]; 
}]; 

也有一些方法來排序代替可變數組類似於以上。

編輯:我知道還有其他方法可以使用較少的代碼來做到這一點,但我確實更喜歡各種事物的塊枚舉器,所以我發現它在實踐中保持有用。你也可以調整這些以使用更多的非標準排序(整數與對象),並使用NSLog()...實際記錄發生的情況。

相關問題