2011-12-12 130 views
1

我有以下的代碼,通過排序上升。有人可以幫我解決我遇到的NSSortDescriptor問題嗎?

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"platform.name" ascending:YES]; 
    NSMutableArray *sortedReleases = [NSMutableArray arrayWithArray:[theReleases sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]]; 
    [sortDescriptor release]; 

我想要做的是做一個排序,其中:

  1. 顯示,其中一個sortedRelease被判真正的那些

  2. 排序使用acsending休息(它是如何做目前)

我該怎麼做呢?

回答

2

方法sortedArrayUsingDescriptors:sortDescriptors是多個,並且帶有一個數組所以它意味着可以有這將在它們出現在陣列中的順序來應用多個排序描述符。

下面的代碼會做那種你想如何提供我瞭解你的權利,你有物業sortedRelease

NSSortDescriptor *sortBySortedRelease = [[NSSortDescriptor alloc] initWithKey:@"sortedRelease" ascending:NO]; 
NSSortDescriptor *sortByName   = [[NSSortDescriptor alloc] initWithKey:@"platform.name" ascending:YES]; 

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortBySortedRelease, sortByName, nil]; 

[sortBySortedRelease release]; sortBySortedRelease = nil; 
[sortByName release]; sortByName = nil; 

NSMutableArray *sortedReleases = [NSMutableArray arrayWithArray:[theReleases sortedArrayUsingDescriptors:sortDescriptors]]; 
[sortDescriptors release]; sortDescriptors = nil; 

下面是從文檔的相關章節NSArray

- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors 

參數
sortDescriptors
NSSortDescriptor對象的數組。

返回值
按sortDescriptors指定的接收數組的副本。

討論
第一個描述符指定的主鍵路徑在分揀接收陣列的內容被使用。任何後續的描述符都用於進一步優化具有重複值的對象的排序。有關更多信息,請參閱NSSortDescriptor。

+0

如果我沒有獲得sortedRelease和需要評估複雜的功能? – jini

+0

如果它很複雜很可能是該類應該有它計算的負有責任。因此,只需讓班級計算它並通過只讀屬性使其可用即可。 –

相關問題