How do I alphabetically sort a custom object field within a NSMutable Array?

前端 未结 2 764
悲&欢浪女
悲&欢浪女 2021-02-09 10:57

I have a custom object like:

#import 

@interface Store : NSObject{
    NSString *name;
    NSString *address;
}

@property (nonat         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-09 11:31

    NSSortDescriptor *sortDescriptor =
        [NSSortDescriptor sortDescriptorWithKey:@"name"
                                      ascending:YES
                                       selector:@selector(caseInsensitiveCompare:)];
    [nameOfArray sortedArrayUsingDescriptors:@[sortDescriptor]];
    

    Related documentation:

    • [NSArray sortedArrayUsingDescriptors:]
    • NSSortDescriptor

提交回复
热议问题