How to sort a NSArray alphabetically?

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

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

7条回答
  •  抹茶落季
    2020-11-22 16:13

    Use below code for sorting in alphabetical order:

        NSArray *unsortedStrings = @[@"Verdana", @"MS San Serif", @"Times New Roman",@"Chalkduster",@"Impact"];
    
        NSArray *sortedStrings =
        [unsortedStrings sortedArrayUsingSelector:@selector(compare:)];
    
        NSLog(@"Unsorted Array : %@",unsortedStrings);        
        NSLog(@"Sorted Array : %@",sortedStrings);
    

    Below is console log :

    2015-04-02 16:17:50.614 ToDoList[2133:100512] Unsorted Array : (
        Verdana,
        "MS San Serif",
        "Times New Roman",
        Chalkduster,
        Impact
    )
    
    2015-04-02 16:17:50.615 ToDoList[2133:100512] Sorted Array : (
        Chalkduster,
        Impact,
        "MS San Serif",
        "Times New Roman",
        Verdana
    )
    

提交回复
热议问题