Sort XML Data Coordinates (Example: SeismicXML)

Discussion in 'iOS Programming' started by Darkroom, Sep 28, 2009.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    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?
     
  2. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #2
    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:)];
    
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    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.
     
  4. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    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. :eek:
     

Share This Page