NSMutableArray
のに基づいNSMutableDictionary
て、キーに基づいて昇順に並べ替えようとします。キーが呼び出されProduct Sale Price
、サーバーから のような文字列として返されます$350
。したがって、最初の文字を部分文字列にして int 値を比較します。
//Sorting function
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];
NSLog(@"v1, v2: %i | %i",v1,v2);
if (v1 > v2){
NSLog(@"v2 is smaller: <%i>",v2);
return v2;
}
else if (v1 < v2){
NSLog(@"v1 is smaller: <%i>",v1);
return v1;
}
else
return NSOrderedSame;
}
//Somewhere in the code
arrayProduct = (NSMutableArray*)[arrayProduct sortedArrayUsingFunction:priceComparator context:nil];
NSLog(@"%@",arrayProduct);//The array is not sorted as expected, still random order
したがって、基本的には、そのステップ b ステップをデバッグし、すべての比較が正しいにもかかわらず、順序に何らかの影響はありません。何か不足していますか?
編集:
ここに のいくつかの項目がありarrayProduct
ます:
(
{
"Product ID" = 15119;
"Product Sale Price" = "$395";
},
{
"Product ID" = 16897;
"Product Sale Price" = "$75";
}
)