Sorting array(NSArray) in descending order

后端 未结 3 860
别跟我提以往
别跟我提以往 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:12

    You can use NSSortDescriptor:

    NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:@selector(localizedCompare:)];
    NSArray* sortedArray = [inFileTypes sortedArrayUsingDescriptors:@[sortDescriptor]];
    

    Here we use localizedCompare: to compare the strings, and pass NO to the ascending: option to sort in descending order.

提交回复
热议问题