【问题标题】:Sort NSDictionary in ascending order按升序对 NSDictionary 进行排序
【发布时间】:2013-05-30 14:43:48
【问题描述】:

我正在尝试按升序对NSDictionary 进行排序。我正在使用此代码:

NSDictionary *valDict = self.mGetDataDict[key][rowKey];

for (NSString *valueKey in
     [[valDict allKeys] sortedArrayUsingSelector:@selector(compare:)])
{                
    if ([valueKey isEqualToString:@"attr"])
    {
        dictRow = self.mGetDataDict[key][rowKey][valueKey];
    }
    else {
        NSString *valKey = self.mGetDataDict[key][rowKey][valueKey];
        [arrSeatsStatus addObject:valKey];
    }
}

这是我得到的输出:

1 = off;
10 = off;
2 = on;
3 = on;
4 = on;
5 = on;
6 = on;
7 = on;
8 = on;
9 = on;

这是所需的输出:

1: "off",
2: "on",
3: "on",
4: "on",
5: "on",
6: "on",
7: "on",
8: "on",
9: "on",
10: "off"

所需的输出是来自 JSON 的实际值。

【问题讨论】:

    标签: iphone ios objective-c json sorting


    【解决方案1】:

    你可以像这样使用 NSSortDescriptor:

    NSArray* array1 = @[@"1 = off", @"10 = off", @"2 = on", @"3 = on"];
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"" ascending:YES selector:@selector(localizedStandardCompare:)];
    
    NSLog(@"Ordered array: %@", [array1 sortedArrayUsingDescriptors:@[ descriptor ]]);
    

    产生这个输出:

    2013-06-04 12:26:22.039 EcoverdFira[3693:c07] Ordered array: (
      "1 = off",
      "2 = on",
      "3 = on",
      "10 = off"
    )
    

    NSSortedDescriptorhere上有一篇好文章。

    【讨论】:

    • 如何将其标记为正确答案?这甚至没有对字典进行排序,它只是对数组进行排序!
    【解决方案2】:

    尝试使用这个....

    //变化.......

    NSArray *valDict = [[self.mGetDataDict allValues] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSMutableDictionary *orderedDictionary=[[NSMutableDictionary alloc] init];
    
    for (NSString *valor in valDict)
    {
        for (NSString *clave in [yourDictionary allKeys])
        {
            if ([valor isEqualToString:[valDict valueForKey:clave]]) 
            {
                [orderedDictionary setValue:valor forKey:clave];
            }
        }
    }
    

    【讨论】:

    • 在第一行的实例中发送错误无法识别的选择器。
    • 这不是问题,而是 [dict allKeys] 来而不是值。问题是即使 valor == clave control 没有进入。
    • 你的意思是要根据 Key 进行搜索?
    • 等我试过了,几分钟后告诉你。
    • 这个东西不对key进行排序,是的,我需要基于key。
    【解决方案3】:

    获取值的数组,对该数组进行排序,然后获取与该值对应的键。

    您可以通过以下方式获取值:

    NSArray* values = [myDict allValues];
    NSArray* sortedValues = [values sortedArrayUsingSelector:@selector(comparator)];
    

    但是,如果集合如您在示例中显示的那样(我的意思是,您可以从键中推断出值),您始终可以对键进行排序,而不是弄乱值。

    使用:

    NSArray* sortedKeys = [myDict keysSortedByValueUsingSelector:@selector(comparator)];
    

    比较器是一个消息选择器,它被发送到您要订购的对象。

    如果你想对字符串排序,那么你应该使用 NSString 比较器。 NSString 比较器是:caseInsensitiveCompare 或localizedCaseInsensitiveCompare:。

    如果这些都不适合你,你可以调用你自己的比较器函数

    [values sortedArrayUsingFunction:comparatorFunction context:nil]
    

    作为comparatorFunction(来自AppleDocumentation

    NSInteger intSort(id num1, id num2, void *context)
    {
        int v1 = [num1 intValue];
        int v2 = [num2 intValue];
        if (v1 < v2)
            return NSOrderedAscending;
        else if (v1 > v2)
            return NSOrderedDescending;
        else
            return NSOrderedSame;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 2011-12-08
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多