I'm trying to sort a list of CLLocationDistance
values by closest distance (ascending order). I first converted the values to NSString
objects so that I could use the following sort:
NSArray *sortedArray;
sortedArray = [distances sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSString cannot sort on the numeric value.
How can I set a list of numeric values in ascending order?
I think what you want is sortedArrayUsingFunction:context:
using something like this:
NSInteger intSort(id num1, id num2, void *context)
{
int v1 = [num1 intValue];
int v2 = [num2 intValue];
if (v1 < v2)
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}
// sort things
NSArray *sortedArray; sortedArray = [anArray sortedArrayUsingFunction:intSort context:NULL];
apple docs reference
Try using sortedArrayUsingSelector where you define the function to compare the elements heres a reference :http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/sortedArrayUsingSelector:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With