我试图先按价格然后按标题对大量产品进行排序,然后显示它们:
应用这两种排序似乎不起作用,但是,应用其中一种效果很好,所以如果我应用按价格排序,它将返回按价格排序的产品,标题也是如此。那么为什么我不能同时按价格和标题排序呢?
//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;
}