Sorting NSMutableArray By Object's Property

前端 未结 2 1566
我寻月下人不归
我寻月下人不归 2020-12-17 07:22

So this is a rather basic question regarding the best way to sort an NSMutableArray of custom objects.

I have a an NSMutableArray of custom

相关标签:
2条回答
  • 2020-12-17 08:04

    This little snippet worked great for me:

    [students sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]];
    
    0 讨论(0)
  • 2020-12-17 08:22

    NSSortDescriptorss make this really simple. With NSMutableArray you can sort the existing array using sortUsingDescriptors: and with immutable arrays you create a new array using sortedArrayUsingDescriptors:

    //This will sort by stringProperty ascending, then dateProperty ascending
    [mutable_array sortUsingDescriptors:
     @[
      [NSSortDescriptor sortDescriptorWithKey:@"stringProperty" ascending:YES],
      [NSSortDescriptor sortDescriptorWithKey:@"dateProperty" ascending:YES]
      ]];
    
    0 讨论(0)
提交回复
热议问题