【问题标题】:Sort array of dictionaries in ios在ios中对字典数组进行排序
【发布时间】:2014-11-10 05:10:12
【问题描述】:

我有一个包含多个字典的数组。 见下面的例子:

示例:

Main Array:

(
    {
        Title: "A";
        Value: 1;
        Priority: 
        (
            4  //Problem: contains array as value which need to be sorted
        );
    },
    {
        Title: "B";
        Value: 2;
        Priority: 
        (
            3 //Problem: contains array as value which need to be sorted
        );
    },
    {
        Title: "C";
        Value: 3;
    },
    {
        Title: "D";
        Value: 4;
        Priority: 
        (
        );
    },
    {
        Title: "E";
        Value: 5;
        Priority: null
    },
)

我需要根据字典中的“优先级”对主数组进行排序。 优先级值是一个数组。

我怎样才能实现这种排序?

帮我解决这个问题...

提前致谢

【问题讨论】:

  • 你的优先级是字符串还是数字?
  • @Rob,实际上我必须从 String 转换为 Integer 然后对它们进行排序。

标签: ios iphone arrays sorting dictionary


【解决方案1】:

您可以使用NSSortDescriptor

NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:@"Title" ascending:YES];
[arr sortUsingDescriptors:[NSArray arrayWithObject:desc]];

或者其他选项可以是sortedArrayUsingComparator

[arr sortedArrayUsingComparator:^(NSDictionary *item1, NSDictionary *item2) {
    NSString *first = [item1 objectForKey:@"Title"];
    NSString *second = [item2 objectForKey:@"Title"];
    return [first compare:second options:NSLiteralSearch];
}];

【讨论】:

  • 感谢回复...但我想按“优先级”排序。
  • 你优先存储的内容
  • 那么你的排序规则是什么?基于每个优先级数组中的最小值?最高的?平均值?
  • @Paulw11,感谢您的回复……首先是最高的意愿。
  • 如果优先键包含单个值为什么你使用数组?
【解决方案2】:

试试这个。如果你需要相反的,那么只需改变 if-else 条件的顺序

[mainArray sortedArrayUsingComparator:^(NSDictionary *dic1, NSDictionary *dic2) {
    int priority1 = [[[dic1 objectForKey:@"Priority"] objectAtIndex:0] intValue];
    int priority2 = [[[dic2 objectForKey:@"Priority"] objectAtIndex:0] intValue];

    if(priority1 < priority2) return NSOrderedAscending;
    else if(priority1 > priority2) return NSOrderedDescending;
    else return NSOrderedSame;

}];

【讨论】:

    【解决方案3】:
            NSArray *mainArray = @[
                           @{
                               @"Title":@"A",
                               @"value":@"1",
                               @"priority":@[[NSNumber numberWithInt:4]]
                            },
    
                           @{
                               @"Title":@"F",
                               @"value":@"1",
                               @"priority":@[[NSNumber numberWithInt:1]]
                            },
    
                           @{
                               @"Title":@"E",
                               @"value":@"1",
                               @"priority":@[]
                               },
    
                           @{
                               @"Title":@"C",
                               @"value":@"1",
                               @"priority":@[[NSNumber numberWithInt:3]]
                            },
    
                           @{
                               @"Title":@"D",
                               @"value":@"1",
                               @"priority":@[[NSNumber numberWithInt:5]]
                            },
    
                           @{
                               @"Title":@"B",
                               @"value":@"1",
                               @"priority":@[[NSNumber numberWithInt:2]]
                            },
    
                           @{
                               @"Title":@"E",
                               @"value":@"1",
                               @"priority":@[]
                            },
    
                           @{
                               @"Title":@"G",
                               @"value":@"1",
                               @"priority":[NSNull null]
                            }
                           ];
    
    
    
      NSArray *sortedArray =  [mainArray sortedArrayUsingComparator:^(NSDictionary *dic1, NSDictionary *dic2) {
    
            int priority1 = 0,priority2 = 0;
    
            if([[dic1 objectForKey:@"priority"] isKindOfClass:[NSArray class]]){
                NSArray *arr = (NSArray *)[dic1 objectForKey:@"priority"];
                if(arr.count != 0)
                    priority1 = [[arr objectAtIndex:0] intValue];
            }
    
            if([[dic2 objectForKey:@"priority"] isKindOfClass:[NSArray class]]){
                NSArray *arr = (NSArray *)[dic2 objectForKey:@"priority"];
                if(arr.count != 0)
                    priority2 = [[arr objectAtIndex:0] intValue];
            }
    
            if(priority1 > priority2) return NSOrderedAscending;
            else if(priority1 < priority2) return NSOrderedDescending;
            else return NSOrderedSame;
    
        }];
    
    
    
    > The OutPut is in my LOG:
    > 
    > 2014-11-10 12:09:20.925 Sort Array[1624:591502] sorted array: (
    >         {
    >         Title = D;
    >         priority =         (
    >             5
    >         );
    >         value = 1;
    >     },
    >         {
    >         Title = A;
    >         priority =         (
    >             4
    >         );
    >         value = 1;
    >     },
    >         {
    >         Title = C;
    >         priority =         (
    >             3
    >         );
    >         value = 1;
    >     },
    >         {
    >         Title = B;
    >         priority =         (
    >             2
    >         );
    >         value = 1;
    >     },
    >         {
    >         Title = F;
    >         priority =         (
    >             1
    >         );
    >         value = 1;
    >     },
    >         {
    >         Title = E;
    >         priority =         (
    >         );
    >         value = 1;
    >     },
    >         {
    >         Title = E;
    >         priority =         (
    >         );
    >         value = 1;
    >     },
    >         {
    >         Title = G;
    >         priority = "<null>";
    >         value = 1;
    >     } )
    

    【讨论】:

    • 如果任何人的答案比我的答案更有效,请告诉我,我将不胜感激:)
    猜你喜欢
    • 2015-03-07
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    相关资源
    最近更新 更多