2015-07-03 36 views
0

一個數組包含NSDate對象,如何使用排序描述符對它們進行排序?我想下面的代碼:排序描述符對NSArray中的nsdate對象不起作用。

NSSortDescriptor * dateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Date" ascending:YES]; 

NSArray * sortedArray = [allDates addObjectsFromArray:[allDates sortedArrayUsingDescriptors:@[dateSortDescriptor]]]; 
+0

的可能重複的[如何排序日期對象的NSMutableArray](http://stackoverflow.com/questions/13667288/how-to-sort-nsmutablearray-of-date-objects) – n00bProgrammer

回答

2

的NSDate符合compare:方法和上升是默認順序

NSArray *sortedArray = [allDates sortedArrayUsingSelector:@selector(compare:)]; 
0

@Avt,回答了這個問題,我不知道他爲什麼刪除它。

NSSortDescriptor * dateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"timeIntervalSince1970" ascending:YES]; 

NSArray * sortedArray = [allDates addObjectsFromArray:[allDates sortedArrayUsingDescriptors:@[dateSortDescriptor]]]; 
+0

@Avt 請將您的答案再次發佈其工作正常:) – Zubair

+1

在任何情況下,@vadian都有一個很好的做法。沒有必要的自定義sortDescriptor。 – stevesliva