如何根据其关键的“id”升序对这个NSDictionary进行排序?

时间:2016-04-08 15:05:50

标签: ios objective-c sorting nsdictionary

嗨这是从服务器获取的内容

{
        1 =         {

            "display_name" = "One";
            id = 1;
     };
      2 =         {

            "display_name" = "Two";
            id = 2;
     };
      13 =         {

            "display_name" = "abc";
            id = 13;
     };
      15 =         {

            "display_name" = "aaa";
            id = 15;
     };
     4 =         {

            "display_name" = "ffd";
            id = 4;
     };
      3 =         {

            "display_name" = "abdfdfc";
            id = 3;
     };
      5 =         {

            "display_name" = "aasdfsdfa";
            id = 5;
     };
}

我需要根据“id”对此进行排序这是我看起来的输出

期待输出

{
        1 =         {

            "display_name" = "One";
            id = 1;
     };
      2 =         {

            "display_name" = "Two";
            id = 2;
     };
      3 =         {

            "display_name" = "abdfdfc";
            id = 3;
     };
      4 =         {

            "display_name" = "ffd";
            id = 4;
     };
     5 =         {

            "display_name" = "aasdfsdfa";
            id = 5;
     };
      13 =         {

            "display_name" = "abc";
            id = 13;
     };
      15 =         {

            "display_name" = "aaa";
            id = 15;
     };
}

此代码我已尝试过,但无法正常工作

 //vehiclesDictionary   real dictionary
        NSMutableArray *sortedKeys=[[NSMutableArray alloc]init];
         for(NSString *item in [vehiclesDictionary allKeys]){
             [sortedKeys addObject:[NSNumber numberWithInt:[item intValue]]];
         }


         NSArray *sortedKeysArray = [sortedKeys sortedArrayUsingSelector: @selector(compare:)];
         NSLog(@"%@",sortedKeysArray);

         NSMutableDictionary *sortedValues = [[NSMutableDictionary alloc] init];
         for (NSString *key in sortedKeysArray) {
             [sortedValues setValue:[vehiclesDictionary valueForKey:[NSString stringWithFormat:@"%@",key]] forKey:key];
         }

         NSLog(@"%@",sortedValues);

请帮助我

2 个答案:

答案 0 :(得分:1)

您无法对NSDictionary进行排序,它是一种未排序的集合类型。您需要将密钥存储在一个数组中并对其进行排序,并使用它来按顺序访问NSDictionary。

根据您的上述代码,可以按如下方式对其进行修改,例如

NSDictionary *dict = [NSDictionary dictionary];

NSArray *sortedKeys = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];

for (NSString *key in sortedKeys) {
    NSLog(@"%@", [d objectForKey:key]);

    // Do something with the object here
}

在这里,您可以使用NSDictionary传递sortedKeys数组,并使用sortedKeys数组按顺序访问NSDictionary。

获得数组的更简洁的方法,但与上面相同的结果,将使用:

NSDictionary -keysSortedByValueUsingComparatorhere所示。

答案 1 :(得分:0)

正如其他人所说,NSDictionaries无法排序。但是,您可以这样做:

-(NSArray *)sortedKeysFromDictionary:(NSDictionary *)dictionary ascending:(BOOL)ascending
{
    /* get all keys from dictionary */
    NSArray *allKeys = [dictionary allKeys];

    NSString *key = @"id"; // using "id" as key here

    /* sort keys */
    NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:key ascending:ascending];
    return [NSArray arrayWithArray:[allKeys sortedArrayUsingDescriptors:@[dateDescriptor]]];
}

这将从您的词典中获取所有键,根据需要按升序或降序对其进行排序,并将其作为NSArray返回。然后可以使用此数组来访问原始字典。示例实现看起来像这样:

for (NSString *key in [self sortedKeysFromDictionary:sampleDic ascending:NO])
{
    /* get value from current key */
    NSDictionary *currentDic = [sampleDic objectForKey:key];
}