I want to sort a NSMutableArray, where each row is a NSMutableDictionary, with my GPS position from CoreLocation framework.
This is an example of my array of POI
arrayCampi = (
{
    cap = 28100;
    "cell_phone" = "";
    championship = "IBL 1D";
    citta = Novara;
    division = "";
    email = "";
    fax = 0321457933;
    indirizzo = "Via Patti, 14";
    latitude = "45.437174";
    league = "";
    longitude = "8.596029";
    name = "Comunale M. Provini";
    naz = Italy;
    prov = NO;
    reg = Piemonte;
    sport = B;
    surname = "Elettra Energia Novara 2000";
    telefono = 03211816389;
    webaddress = "http://www.novarabaseball.it/";
})
I need to sort this array with my location (lat and long) with field 'latitude' and 'longitude' of each row in ascending mode (first row is POI nearest to me).
I have tried this solution without success:
+ (NSMutableArray *)sortBallparkList:(NSMutableArray *)arrayCampi location:(CLLocation *)myLocation  {
    if ([arrayCampi count] == 0) {
        return arrayCampi;
    }
    if (myLocation.coordinate.latitude == 0.00 &&
        myLocation.coordinate.longitude == 0.00) {
        return arrayCampi;
    }
    NSMutableArray *sortedArray = [NSMutableArray arrayWithArray:arrayCampi];
    BOOL finito = FALSE;
    NSDictionary *riga1, *riga2;
    while (!finito) {
        for (int i = 0; i < [sortedArray count] - 1; i++) {
            finito = TRUE;
            riga1 = [sortedArray objectAtIndex: i];
            riga2 = [sortedArray objectAtIndex: i+1];
            CLLocationDistance distanceA = [myLocation distanceFromLocation:
                                            [[CLLocation alloc]initWithLatitude:[[riga1 valueForKey:@"latitude"] doubleValue]                                             
                                                                      longitude:[[riga1 valueForKey:@"longitude"] doubleValue]]];
            CLLocationDistance distanceB = [myLocation distanceFromLocation:
                                            [[CLLocation alloc]initWithLatitude:[[riga2 valueForKey:@"latitude"] doubleValue]
                                                                      longitude:[[riga2 valueForKey:@"longitude"] doubleValue]]];
            if (distanceA > distanceB) {
                [riga1 retain];
                [riga2 retain];
                [sortedArray replaceObjectAtIndex:i+1 withObject:riga2];
                [sortedArray replaceObjectAtIndex:i withObject:riga1];
                [riga1 release];
                [riga2 release];
                finito = FALSE;
            }
        }
    }
    return sortedArray;
}
Can anyone help me, also with other solution?
Alex.
 
     
     
     
    