基於NSMutableDictionary
s的NSMutableArray
s,我嘗試基於按升序排列的鍵進行排序。該密鑰被稱爲Product Sale Price
,它從服務器返回,如$350
這樣的字符串。所以我字符串的第一個字符來比較int類型:sortedArrayUsingFunction沒有正確排序
//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";
}
)