How to sort a NSArray alphabetically?

前端 未结 7 1094
借酒劲吻你
借酒劲吻你 2020-11-22 15:32

How can I sort an array filled with [UIFont familyNames] into alphabetical order?

相关标签:
7条回答
  • 2020-11-22 16:35

    The other answers provided here mention using @selector(localizedCaseInsensitiveCompare:) This works great for an array of NSString, however if you want to extend this to another type of object, and sort those objects according to a 'name' property, you should do this instead:

    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    sortedArray=[anArray sortedArrayUsingDescriptors:@[sort]];
    

    Your objects will be sorted according to the name property of those objects.

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

    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    
    0 讨论(0)
提交回复
热议问题