Your "delta" appears to be the absolute value of the difference between "rSize" and the item. In that case, you can perform the subtraction (item – rSize
), and multiply it by -1 if it is negative entirely within NSDecimalNumber
:
NSDecimalNumber *negativeOne = [NSDecimalNumber decimalNumberWithMantissa:1
exponent:0
isNegative:YES];
NSDecimalNumber *delta = [item decimalNumberBySubtracting:rSize];
if ([delta compare:[NSDecimalNumber zero]] == NSOrderedAscending) {
delta = [delta decimalNumberByMultiplyingBy:negativeOne];
}
Then use the -compare:
selector on the resulting delta objects to sort your array of objects.