How can I sort strings in NSMutableArray into alphabetical order?

后端 未结 4 709
暖寄归人
暖寄归人 2020-12-24 11:26

I have a list of strings in an NSMutableArray, and I want to sort them into alphabetical order before displaying them in my table view.

How can I do tha

4条回答
  •  天命终不由人
    2020-12-24 11:42

    It's easy to get the ascending order. Follow the bellow steps,,,

    Model 1 :

    NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    sortedArray=[yourArray sortedArrayUsingDescriptors:@[sortDesc]];
    

    If you want the sorting to be case insensitive, you would need to set the descriptor like this,,
    Model 2 :

    NSSortDescriptor * sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 
    sortedArray=[yourArray sortedArrayUsingDescriptors:@[sortDesc]];
    

提交回复
热议问题