Objective c 对词典进行排序

Objective c 对词典进行排序,objective-c,xcode,Objective C,Xcode,我正在阅读这个JSON文件: {"longitude":["36.704765","46.704765", "47.704765"],"latitude":["-93.168274","-103.168274", "86.704765"]} 这样: NSString *filenameMap = [NSString stringWithFormat:@"%@%@Map", destDir, NavBar.topItem.title]; NSString *MapPath = [documen

我正在阅读这个JSON文件:

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

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];
JSON文件中的每对坐标(纬度和经度)在MapView上绘制一个注释,因此我有3个不同的地图视图,每个视图有一个管脚。但是地图应该是有序的:所以第一张地图必须显示第一对坐标,第二张地图,第二对坐标…当我按下刷新时,地图的顺序会改变,如果我按下刷新,地图的顺序会再次改变

所以我希望JSON文件中的坐标顺序得到尊重

我试过了,但没有办法:

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

编辑:

那么我喜欢这样:

 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];


     }
NSArray*allKeys=[Dictionary allKeys];
对于(int i=0;i<[所有键计数];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添加子视图:mapView2];
NSString*key2=[allKeys2 objectAtIndex:i];
NSObject*obj2=[DictionaryMapObjectForkey:key2];
NSString*地址=[NSString stringWithFormat:@“%@”,obj2];
float stringFloat=[地址floatValue];
float-stringFloat2=[key2-floatValue];
CLLocationCoordination2D任意位置;
anyLocation.longitude=stringFloat;
anyLocation.latitude=2;
MKPointAnnotation*annotationPoint2=[[MKPointAnnotation alloc]init];annotationPoint2.coordinate=anyLocation;
annotationPoint2.title=@“事件”;
annotationPoint2.subtitle=@“微软总部2”;
[mapView2添加注释:注释点2];
[mapView2.userLocation setTitle:@“我在这里”];
[mapView2.userLocation addObserver:self
分叉路径:@“位置”
选项:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
上下文:NULL];
[地图视图2设置显示位置:否];
如果(MapViewArray==nil)MapViewArray=[[NSMutableArray alloc]init];
[MapViewArray addObject:mapView2];
}

因此,如果这些值是统一的,我如何才能命名经度和纬度值???

字典没有顺序的概念,因此如果您需要原始顺序的经度/纬度对,那么一个字典不是合适的数据结构

您可以通过多种方式解决此问题,其中之一是创建一个字典数组,其中每个字典表示一对经度/纬度:

//...
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]);
    //...
}
/。。。
NSArray*mapArrayLongitude=[DataMapObjectForkey:@“经度”];
NSArray*mapArrayLatitude=[DataMapObjectForkey:@“纬度”];
if(mapArrayLongitude.count!=maparrayLengitude.count){
//错误:经度/纬度值的数量不等
}否则{
NSMutableArray*对=[NSMutableArray];
对于(int i=0;i
您还可以简单地保留这两个数组,并在以后通过访问两个数组中的相同索引来重建这两个数组对