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

前端 未结 27 3254
予麋鹿
予麋鹿 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:14

    I just done multi level sorting based on custom requirement.

    //sort the values

        [arrItem sortUsingComparator:^NSComparisonResult (id a, id b){
    
        ItemDetail * itemA = (ItemDetail*)a;
        ItemDetail* itemB =(ItemDetail*)b;
    
        //item price are same
        if (itemA.m_price.m_selling== itemB.m_price.m_selling) {
    
            NSComparisonResult result=  [itemA.m_itemName compare:itemB.m_itemName];
    
            //if item names are same, then monogramminginfo has to come before the non monograme item
            if (result==NSOrderedSame) {
    
                if (itemA.m_monogrammingInfo) {
                    return NSOrderedAscending;
                }else{
                    return NSOrderedDescending;
                }
            }
            return result;
        }
    
        //asscending order
        return itemA.m_price.m_selling > itemB.m_price.m_selling;
    }];
    

    https://sites.google.com/site/greateindiaclub/mobil-apps/ios/multilevelsortinginiosobjectivec

提交回复
热议问题