按多个键排序字典

时间:2017-03-01 08:26:37

标签: objective-c sorting nsdictionary

我的myDict格式为:

{
  data1 = { 
             key1 = abc
             key2 = abc
             key3 = abc
             key4 = abc
          },
  data2 = { 
             key1 = abc
             key2 = abc
             key3 = abc
             key4 = abc
          }
  ..
}

我使用以下代码对myDict进行排序,并通过键获取排序数据。

    sortedArr = [myDict keysSortedByValueUsingComparator:
                 ^NSComparisonResult(NSDictionary* obj1, NSDictionary* obj2)
                 {
                     NSComparisonResult result = NSOrderedSame;
                     if(obj1[@"key1"] != nil && obj2[@"key1"]!=nil)
                     {
                         return [obj2[@"key1"] compare:obj1[@"key1"]];
                     }
                     else
                         return result;
                 }];

sortedArr:

(data1,data2,data3,...)

问题是如何使用2个键而不是1个键对myDict进行排序,因为如果key1值相同,我会遇到问题,那么我希望它按key2排序。

2 个答案:

答案 0 :(得分:3)

您只需要考虑比较返回NSOrderedSame的每个键的大小写。您可以像这样迭代键:

NSArray *allKeys = @[@"key1", @"key2", @"key3", ...];
for (NSString *aKey in allKeys)
{
    if(obj1[aKey] != nil && obj2[aKey] != nil)
    {
        NSComparisonResult result = [obj2[aKey] compare:obj1[aKey]]; 
        if (result != NSOrderedSame) return result;
    }
}
return NSOrderedSame;

这个答案与我原来的答案有很大的不同,这个解决方案的所有归功于Larme

答案 1 :(得分:0)

您不能将nil插入集合(字典,数组,索引集等)。

但是,您可以将[NSNull null]插入其中,因为这是他们为

所做的

下面你可以找到用subDictinaries中所有键对第一级词典进行排序的代码。

NSMutableDictionary *md = [NSMutableDictionary dictionary];

NSMutableDictionary *data1 = [NSMutableDictionary dictionary];

[data1 setObject:@"abc" forKey:@"key1"];
[data1 setObject:@"abc" forKey:@"key2"];
[data1 setObject:@"abc" forKey:@"key3"];
[data1 setObject:@"a" forKey:@"key4"];

NSMutableDictionary *data2 = [NSMutableDictionary dictionary];

[data2 setObject:@"abc" forKey:@"key1"];
[data2 setObject:@"abc" forKey:@"key2"];
[data2 setObject:@"abc" forKey:@"key3"];
[data2 setObject:@"b" forKey:@"key4"];

NSMutableDictionary *data3 = [NSMutableDictionary dictionary];

[data3 setObject:@"abc" forKey:@"key1"];
[data3 setObject:@"abc" forKey:@"key2"];
[data3 setObject:@"abc" forKey:@"key3"];
[data3 setObject:@"c" forKey:@"key4"];

[md setObject:data1 forKey:@"data1"];
[md setObject:data2 forKey:@"data2"];
[md setObject:data3 forKey:@"data3"];

NSArray *allKeys = @[@"key1", @"key2", @"key3", @"key4"];

NSArray *sortedData = [md keysSortedByValueUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {

    for (NSString *key in allKeys) {

        if (obj1[key] != nil && obj2[key] != nil) { // Checking if both keys exist

            NSComparisonResult result = [obj2[key] compare:obj1[key]];

            if (result != NSOrderedSame) {

                return result;

            }

        } else { // One of keys is missing

            return NSOrderedSame;

        }

    }

    return NSOrderedSame; // No keys

}];

NSLog(@"%@", sortedData);

输出:

(     数据3,     数据2,     数据1 )