Sort an NSArray in Descending Order

前端 未结 11 1209
死守一世寂寞
死守一世寂寞 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: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.

提交回复
热议问题