NSMutableArray
이 NSMutableDictionary
인 경우이를 기준으로 오름차순으로 정렬하려고합니다. 키는 Product Sale Price
이라고하며 서버에서 $350
과 같은 문자열로 반환됩니다. 그래서 INT 값을 비교하는 첫 번째 문자를 하위 문자열 : 그 단계 b 단계를 디버깅하고 모든 비교는 정확하지만 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
그래서 기본적으로, 순서는 어떻게 든 영향을받지 않습니다. 내가 놓친 게 있니?
편집 : 여기
는 arrayProduct
의 일부 항목입니다 :
(
{
"Product ID" = 15119;
"Product Sale Price" = "$395";
},
{
"Product ID" = 16897;
"Product Sale Price" = "$75";
}
)