I am trying to sort a large range of products by price first then by title, then display them:
Applying both sorting doesn't seems to work, HOWEVER, applying one sort of them works fine, so if I apply sorting by price, it will return products sorted by price, the same thing for title. So why I am not able to sort by both price and title?
//Sort by Price, then by Title. Both Ascending orders
arrayProduct = (NSMutableArray*)[arrayProduct sortedArrayUsingFunction:priceComparator context:nil];
arrayProduct = (NSMutableArray*)[arrayProduct sortedArrayUsingFunction:titleComparator context:nil];
//products comparators
NSInteger priceComparator(NSMutableDictionary *obj1, NSMutableDictionary *obj2, void *context){
int v1 = [[[obj1 valueForKey:@"Product Sale Price"]substringFromIndex:1] intValue];
int v2 = [[[obj2 valueForKey:@"Product Sale Price"]substringFromIndex:1] intValue];
if (v1 < v2){
return NSOrderedAscending;
}
else if (v1 > v2){
return NSOrderedDescending;
}
else
return NSOrderedSame;
}
NSInteger titleComparator(NSMutableDictionary *obj1, NSMutableDictionary *obj2, void *context){
NSString* v1 = [obj1 valueForKey:@"Product Title"];
NSString* v2 = [obj2 valueForKey:@"Product Title"];
if ([v1 caseInsensitiveCompare:v2] == NSOrderedAscending){
return NSOrderedAscending;
}
else if ([v1 caseInsensitiveCompare:v2] == NSOrderedDescending){
return NSOrderedDescending;
}
else
return NSOrderedSame;
}