Multi level sorting in iOS (Objective C)

Post date: Jun 4, 2013 9:57:46 AM

I came across the requirement where I need to sort my information based on various condition like

1. Sort the item details in ascending order based on Selling price

2. If selling price are same, then sort the item details in ascending order based on item name

3. If selling price and item name are same then sort based existing of monogramming information

We usually call it as Multi level sorting. I just done it by using below code.

//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;          }]; 

Cheers:), Please let me know if you have queries for the same.