Sorting array(NSArray) in descending order

后端 未结 3 859
别跟我提以往
别跟我提以往 2020-12-25 14:58

I have a array of NSString objects which I have to sort by descending.

Since I did not find any API to sort the array in descending order I approached by following w

3条回答
  •  清酒与你
    2020-12-25 15:26

    or simplify your solution:

    NSArray *temp = [[NSArray alloc] initWithObjects:@"b", @"c", @"5", @"d", @"85", nil];
    NSArray *sortedArray = [temp  sortedArrayUsingComparator:
                            ^NSComparisonResult(id obj1, id obj2){
                                //descending order
                                return [obj2 compare:obj1]; 
                                //ascending order
                                return [obj1 compare:obj2];
                            }];
    NSLog(@"%@", sortedArray);
    

提交回复
热议问题