Вопрос

Due to my requirement, I need to sort my NSMutableArray and need to place unique values in that array.

Here is my code but I am facing a problem that I am getting two arrays: one with distinct values and the other with no distinct values but are sorted.

/ *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);

How can I get both results in one array?

Это было полезно?

Решение

You cannot do it in a single operation, but you can apply the sort descriptor to the result of the uniquing operation:

 NSArray *uniqueArray = [mYearsArray valueForKeyPath:@"@distinctUnionOfObjects.self"];
 NSArray *sortedUniqueArray = [uniqueArray sortedArrayUsingDescriptors:sortDescriptors];
 mYearsArray = [sortedUniqueArray mutableCopy];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top