Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
what is the best course of action to sort XML data that has been parsed into strings, doubles, etc.?? particularly, listing the gps coordinates (doubles) from closest to furthest?
 
i assume you parse your xml data such that you create several objects from the same class, and you store them in an array. i also assume you have a CLLocationCoordinate2D property that stores each object's latitude / longitude. instead of using @synthesize for setting this property, implement your own setter function, that would also create a "distance from here" member variable:
Code:
distanceFromHere = [crtLocation getDistanceFrom:[[CLLocation alloc] initWithLatitude:myObjectsCoordinate.latitude longitude:myObjectsCoordinate.longitude]];
so you reduced the problem to sorting the array of objects by the member called distanceFromHere. in your object's class create an ordering function that you can use for sorting:
Code:
- (NSComparisonResult)compareByDistanceFromHere:(MyClass *)objectICompareWith {
	return (distanceFromHere < objectICompareWith.distanceFromHere) ? NSOrderedAscending : NSOrderedDescending;
}
and finally, considering you added all your objects to a mutable array while parsing, you can use
Code:
[allMyObjects sortUsingSelector:@selector(compareByDistanceFromHere:)];
 
thanks for your help. a few minutes after i posted i actually found this little project where a developer is seasoning apple's SeismicXML example with distance sorting, with plans to include mapkit annotations. his work seems pretty involved for such a simplistic idea, but i guess that's development at its finest.

[EDIT] ouf! your solution seems much more streamlined. this project i am trying to study is a bit crazy and doesn't even seem to work.
 
Last edited by drivefast : Sep 28, 2009 at 11:00 PM. Reason: actually, distanceFromHere must be a property as well - making it a simple member variable is not going to cut it

yeah... in the end i simply created a @property float distanceFromUser; and assigned it the distance between the user's current location and another location with CLLocation's getDistanceFrom: method, and then sorted the array with an NSSortDescriptor before adding it to the tableview.

as usual, in hindsight the solution is quite simple. 😱
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.