Alessandro
Alessandro

Reputation: 4110

Sorting an NSDictionary

I am reading this JSON file:

{"longitude":["36.704765","46.704765", "47.704765"],"latitude":["-93.168274","-103.168274", "86.704765"]}

in this way:

NSString *filenameMap = [NSString stringWithFormat:@"%@%@Map", destDir, NavBar.topItem.title];

NSString *MapPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@Map", NavBar.topItem.title]];

[self.restClient loadFile:filenameMap intoPath:MapPath];


NSString *fileContentMap = [[NSString alloc] initWithContentsOfFile:MapPath];  

SBJsonParser *parserMap = [[SBJsonParser alloc] init];

NSDictionary *dataMap = (NSDictionary *) [parserMap objectWithString:fileContentMap error:nil];  


NSArray *MaparrayLongitude = [dataMap objectForKey:@"longitude"];
NSArray *MaparrayLatitude = [dataMap objectForKey:@"latitude"];

//NSSortDescriptor *MaparrayLongitude = [[NSSortDescriptor alloc] initWithKey:@"longitude"  ascending:NO selector:@selector(compare:)];
//NSSortDescriptor *MaparrayLatitude = [[NSSortDescriptor alloc] initWithKey:@"latitude"  ascending:NO selector:@selector(compare:)];


NSDictionary* DictionaryMap = [NSDictionary dictionaryWithObjects:MaparrayLatitude forKeys:MaparrayLongitude];

Each pair of coordinates in the JSON file (latitude and longitude) plots an annotation on a MapView, so I have 3 different map views, each has 1 pin. But the maps should be in order: so the first map must show the first pair of coordinates, the second map, the second pair of coordinates...Instead when I press refresh the order of the maps changes, and changes again if I repress refresh...

So I want the coordinate order in the JSON file to be respected.

I tried this but no way:

NSSortDescriptor *MaparrayLongitude = [[NSSortDescriptor alloc] initWithKey:@"longitude"  ascending:NO selector:@selector(compare:)];
    NSSortDescriptor *MaparrayLatitude = [[NSSortDescriptor alloc] initWithKey:@"latitude"  ascending:NO selector:@selector(compare:)];

Please help!!

EDIT:

Then I do like this:

 NSArray *allKeys = [Dictionary allKeys];
    for (int i = 0; i < [allKeys count]; i++) {
        NSString *key = [allKeys objectAtIndex:i];
        NSObject *obj = [Dictionary objectForKey:key];

NSArray *allKeys2 = [DictionaryMap allKeys];






CGRect mapFrame = CGRectMake( 400, e, 200, 110);

MKMapView *mapView2 = [[MKMapView alloc] initWithFrame:mapFrame];
[image1 addSubview:mapView2]; 







    NSString *key2 = [allKeys2 objectAtIndex:i];
    NSObject *obj2 = [DictionaryMap objectForKey:key2];




    NSString *address = [NSString stringWithFormat:@"%@", obj2];
    float stringFloat = [address floatValue];
    float stringFloat2 = [key2 floatValue];

    CLLocationCoordinate2D anyLocation;

    anyLocation.longitude = stringFloat;

    anyLocation.latitude  = stringFloat2;







    MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc] init]; annotationPoint2.coordinate = anyLocation;

    annotationPoint2.title = @"Event";
    annotationPoint2.subtitle = @"Microsoft's headquarters2";
    [mapView2 addAnnotation:annotationPoint2];  


    [mapView2.userLocation setTitle:@"I am here"];

    [mapView2.userLocation addObserver:self  
                                 forKeyPath:@"location"  
                                    options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)

                                    context:NULL];

    [mapView2 setShowsUserLocation:NO];

    if (MapViewArray == nil)MapViewArray = [[NSMutableArray alloc]init];
    [MapViewArray addObject: mapView2];


     }

so, if the values are united, how can I annign a longitude and latitude value??

Upvotes: 0

Views: 243

Answers (1)

omz
omz

Reputation: 53561

A dictionary has no concept of order, so if you need your longitude/latitude pairs in the original order, one dictionary is not an appropriate data structure to use.

You could solve this in various ways, one would be to create an array of dictionaries in which each dictionary represents one pair of longitude/latitude:

//...
NSArray *mapArrayLongitude = [dataMap objectForKey:@"longitude"];
NSArray *mapArrayLatitude = [dataMap objectForKey:@"latitude"];
if (mapArrayLongitude.count != mapArrayLatitude.count) {
    //Error: Unequal number of longitude/latitude values
} else {
    NSMutableArray *pairs = [NSMutableArray array];
    for (int i = 0; i < mapArrayLongitude.count; i++) {
        NSDictionary *pair = [NSDictionary dictionaryWithObjectsAndKeys:
                              [mapArrayLongitude objectAtIndex:i], @"longitude",
                              [mapArrayLatitude objectAtIndex:i], @"latitude", nil];
        [pairs addObject:pair];
    }
    NSLog(@"First pair: %@", [pairs objectAtIndex:0]);
    //...
}

You could also simply keep the two arrays and reconstruct the pairs later by accessing the same index in both arrays.

Upvotes: 2

Related Questions