How do I sort an NSMutableArray with custom objects in it?

前端 未结 27 3366
予麋鹿
予麋鹿 2020-11-21 04:45

What I want to do seems pretty simple, but I can\'t find any answers on the web. I have an NSMutableArray of objects, and let\'s say they are \'Person\' objects

27条回答
  •  我寻月下人不归
    2020-11-21 05:18

    In my case, I use "sortedArrayUsingComparator" to sort an array. Look at the below code.

    contactArray = [[NSArray arrayWithArray:[contactSet allObjects]] sortedArrayUsingComparator:^NSComparisonResult(ContactListData *obj1, ContactListData *obj2) {
        NSString *obj1Str = [NSString stringWithFormat:@"%@ %@",obj1.contactName,obj1.contactSurname];
        NSString *obj2Str = [NSString stringWithFormat:@"%@ %@",obj2.contactName,obj2.contactSurname];
        return [obj1Str compare:obj2Str];
    }];
    

    Also my object is,

    @interface ContactListData : JsonData
    @property(nonatomic,strong) NSString * contactName;
    @property(nonatomic,strong) NSString * contactSurname;
    @property(nonatomic,strong) NSString * contactPhoneNumber;
    @property(nonatomic) BOOL isSelected;
    @end
    

提交回复
热议问题