How can I sort an NSMutableArray alphabetically?

后端 未结 6 537
迷失自我
迷失自我 2020-12-08 05:17

I want to sort an NSMutableArray alphabetically.

6条回答
  •  囚心锁ツ
    2020-12-08 05:33

    The other answers provided here mention using @selector(localizedCaseInsensitiveCompare:)
    This works great for an array of NSString, however the OP commented that the array contains objects and that sorting should be done according to object.name property.
    In this case you should do this:

    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    [yourArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];
    

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

提交回复
热议问题