2011-04-10 99 views
4

所以我有下面的代碼,通過「color」屬性對核心數據提取進行排序。在排序的核心數據提取結果中排序?

sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"color" ascending:YES]; 
sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
[fetchRequest setSortDescriptors:sortDescriptors]; 

有沒有辦法取得這種排序的結果,然後按日期(另一個屬性)在每個顏色「組」中進行子排序?

基本上這裏是什麼,我現在得到一個例子...

RED - 11/1/2010 
RED - 9/8/2010 
RED - 11/9/2011 
RED - 10/20/2011 
GREEN - 11/1/2010 
GREEN - 9/8/2010 
GREEN - 11/9/2011 
BLUE - 10/20/2011 
BLUE - 11/1/2010 
BLUE - 9/8/2010 

這裏就是我想結果怎麼看?

RED - 9/8/2010 
RED - 11/1/2010 
RED - 10/20/2011 
RED - 11/9/2011 
GREEN - 9/8/2010 
GREEN - 11/1/2010 
GREEN - 11/9/2011 
BLUE - 9/8/2010 
BLUE - 11/1/2010 
BLUE - 10/20/2011 

我敢肯定,這可以被做,但我不知道如何使它發生。

+0

Stephano,如果您覺得它不再有用,您可以刪除您的問題(使用問題文本下方的「刪除」鏈接)。 – 2011-04-10 03:32:43

回答

10

當你調用SetSortDescriptors時,通過一個你想排序的所有NSSortDescriptors的數組。在你的例子中,你只創建了一個排序描述符,並且只添加了一個排序描述符數組。爲日期字段創建第二個NSSortDescriptor,並將其添加到您的排序描述符數組中。它們按照它們在數組中的順序應用於數據集。從蘋果文檔中看到下面的描述。

像這樣的東西應該工作:

NSSortDescriptor *colorSort = [[NSSortDescriptor alloc] initWithKey:@"color" ascending:asc selector:nil]; 
NSSortDescriptor *dateSort = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:asc selector:nil]; 
NSArray *sortDescriptors = [NSArray arrayWithObjects:colorSort, dateSort, nil]; 
[fetchRequest setSortDescriptors:sortDescriptors]; 

請參見以下鏈接: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSFetchRequest_Class/NSFetchRequest.html

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html

把接收機的排序描述符的數組。 sortDescriptors - 排序描​​述符指定發出讀取請求時返回的對象應如何排序 - 例如按姓氏,然後按名字排序。排序描述符按它們出現在sortDescriptors數組中的順序進行應用(以最低數組 - 索引 - 第一順序串行)。

+0

頁面沒有找到蘋果文件,由好的答案... – AsifHabib 2014-01-30 07:25:00

+0

感謝您指出這@asifhabib。我更新了鏈接。 – mservidio 2014-01-30 18:37:56