Reputation: 1476
I would like to sort an array using NSSortDescriptor, based on a field's intValue. This works perfectly using the following code:
descriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"lowerPrice.floatValue" ascending:YES];
The only issue is that this field either contains a value no matter what, so some of the values are 0. But, in my ranking, the 0's should come as absolute last. Basically, 0.1 should be first, all the way up to let's say 50, and then the 0's. But using floatValue in ascending order, I obviously get the 0's first.
What is the most elegant and simple way to solve this?
Upvotes: 0
Views: 89
Reputation: 53000
Use sortedArrayUsingComparator:
which takes a block which is called to compare pairs of elements and return their order. In the block first test for zeros and return the appropriate ordering (two zeros are equal, a zero is greater than anything else), if there are no zeros return based on true numerical ordering.
HTH
Upvotes: 1