2013-08-05 76 views
1

由於我的要求,我需要對我的NSMutableArray進行排序,並且需要在該數組中放置唯一值。我們可以一次使用排序描述符和@distinctUnionOfObjects嗎?

這是我的代碼,但我面臨着一個問題,我得到兩個數組:一個具有不同的值,另一個沒有明確的值,但是排序。

/ *Making the years unique so that it will display only once*/ 
     mSortingArray = [mYearsArray valueForKeyPath:@"@distinctUnionOfObjects.self"] ; 
     NSLog(@"mSortingArray%@",mSortingArray); 

     /*Sorting the array to get the years in assending format*/ 
     NSSortDescriptor *sortDescriptor; 
     sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"self"ascending:YES]; 
     NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
     [mYearsArray sortUsingDescriptors:sortDescriptors]; 
     NSLog(@"%@ :%@",mYearsArray,sortDescriptors); 

如何在一個數組中獲得兩個結果?

+0

你能顯示輸出? –

回答

6

你不能做一個單一的操作,但可以應用排序描述符的uniquing操作的結果:

NSArray *uniqueArray = [mYearsArray valueForKeyPath:@"@distinctUnionOfObjects.self"]; 
NSArray *sortedUniqueArray = [uniqueArray sortedArrayUsingDescriptors:sortDescriptors]; 
mYearsArray = [sortedUniqueArray mutableCopy]; 
+0

It works thank you – UserDeviOS

+0

@usernew:不客氣! –

相關問題