Sort an NSArray in Descending Order

前端 未结 11 1091
死守一世寂寞
死守一世寂寞 2020-12-08 02:23

I have an NSArray of NSNumber objects that I have successfully sorted in ascending order using the following:

[myArray sortedArrayU         


        
相关标签:
11条回答
  • 2020-12-08 03:04

    The Dr. Touch website has a nice implementation of a category on NSArray to create a self-sorting array. This could be modified to keep the sort in whatever order was appropriate.

    0 讨论(0)
  • 2020-12-08 03:06

    For Example Data is Like this and we want to sort NSArray based on sId key.

    <__NSArrayM 0x7ffc725af1d0>(
    {
        sId = 3;
        vName = ABC;
    },
    {
        sId = 10;
        vName = DEF;
    },
    {
        sId = 9;
        vName = GHI;
    },
    {
        sId = 7;
        vName = JKL;
    },
    {
        sId = 1;
        vName = MNO;
    }
    )
    

    Solution is as Below

    NSArray *sortedArray = [arrOptions sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        if ([[obj1 valueForKey:@"sId"] integerValue] > [[obj2 valueForKey:@"sId"] integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        if ([[obj1 valueForKey:@"sId"] integerValue] < [[obj2 valueForKey:@"sId"] integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
    NSLog(@"Sorted Service Array is ::%@",sortedArray);
    
    0 讨论(0)
  • 2020-12-08 03:07

    we can do it simply by using sortUsingSelector. The code as follows;

    NSMutableArray *arr=[[NSMutableArray alloc]initWithObjects: ..objects.. ];
    //Can also use NSArray.
    [arr sortUsingSelector:@selector(compare:options:)];
    NSString *temp;
    int i,j;
    i=0;
    j=[arr count];
    for(i=0;i<([arr count]-1);i++)
    {
        temp=[arr objectAtIndex:i];
        [arr replaceObjectAtIndex:i withObject:[arr objectAtIndex:j]];
        [arr replaceObjectAtIndex:j withObject:temp];
        j--;
    }
    NSLog(@"New array is:%@",arr);
    

    You may think that WHAT A STUPID ANSWER it is :D :D Actually, am not teasing anyone. But in simple logic, we can sort array in descending order by this way. No need of using any type of descriptors or any other complicated stuffs. And it will be easier for entry level programmers.

    0 讨论(0)
  • 2020-12-08 03:12

    Another way, imo, is also nice: write another reverseCompare with category:

    @implementation NSNumber (Utility)
    
    - (NSComparisonResult)reverseCompare:(NSNumber *)aNumber {
      return [aNumber compare:self];
    }
    

    The good thing is that you can reuse everywhere and don't have to write the loop with reverse iterate. The bad thing is that you have to write more code:)

    0 讨论(0)
  • 2020-12-08 03:16

    I have a mutableArray and want to sort in descending order with "number" key. "number" key is a string like "12345", "12346". This way that I had tried and look very well. Hope help you!

    NSComparisonResult result;
    
    NSArray *arrTem = [multableArr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        if([obj1 valueForKey:@"number"] > [obj2 valueForKey:@"number"])
             result = NSOrderedDescending;
        result = NSOrderedSame;
    }];
    
    return [[NSMutableArray alloc]initWithArray:arrTem];
    
    0 讨论(0)
提交回复
热议问题