如何从字典数组中获取字典的数组(例如arrayGroupBy),其中某个键的值与另一个字典键的值匹配

阿洛克

我有一个包含NSDictionaries的NSMutable差旅,如下所示:

<__NSArrayM 0x7a2b4b00>(
{
"appointment_id" = 235;
"component_name" = Anil;
id = 5;
"start_time" = "2014-11-04 19:00:00";
"time_slot" = "7:00 PM";
},
{
    "appointment_id" = 235;
"component_name" = "test_book";
id = 19;
"start_time" = "2014-11-04 19:00:00";
"time_slot" = "7:00 PM";
},
{
"appointment_id" = 235;
"component_name" = "New Resource";
id = 21;
"start_time" = "2014-11-04 19:00:00";
"time_slot" = "7:00 PM";
},
{
"appointment_id" = 236;
"component_name" = ctbl;
id = 8;
"start_time" = "2014-11-04 22:00:00";
"time_slot" = "10:00 PM";
},
{
"appointment_id" = 236;
"component_name" = btbl;
id = 7;
"start_time" = "2014-11-04 22:00:00";
"time_slot" = "10:00 PM";
  },
{
"appointment_id" = 236;
    "component_name" = "New Resource";
id = 21;
"start_time" = "2014-11-04 22:00:00";
"time_slot" = "10:00 PM";
}
)

我如何从上面的结构中创建一个新数组说arrGroupByTimeSlots像这样:

array of time slot
(
time_slot = (an array of dictionaries with matching time_slot)
time_slot = (an array of dictionaries with matching another time_slot)
)

详细地:

(
time_slot = ({
"appointment_id" = 235;
"component_name" = Anil;
id = 5;
"start_time" = "2014-11-04 19:00:00";
"time_slot" = "7:00 PM";
}, {
"appointment_id" = 235;
"component_name" = "test_book";
id = 19;
"start_time" = "2014-11-04 19:00:00";
"time_slot" = "7:00 PM";
}, {
"appointment_id" = 235;
"component_name" = "New Resource";
id = 21;
"start_time" = "2014-11-04 19:00:00";
"time_slot" = "7:00 PM";
}),

time_slot = ({
"appointment_id" = 236;
"component_name" = ctbl;
id = 8;
"start_time" = "2014-11-04 22:00:00";
"time_slot" = "10:00 PM";
}, {
"appointment_id" = 236;
"component_name" = btbl;
id = 7;
"start_time" = "2014-11-04 22:00:00";
"time_slot" = "10:00 PM";
}, {
"appointment_id" = 236;
"component_name" = "New Resource";
id = 21;
"start_time" = "2014-11-04 22:00:00";
"time_slot" = "10:00 PM";
}))

我尝试过的是:Onik IV提供的解决方案

更多日志:注意“ time_slot” =“ 7:00 PM”; 我要排序的字典数组:

Printing description of arrAllocated:
<__NSArrayM 0x19055f70>(
{
"appointment_id" = 244;
"component_name" = "badge test";
id = 20;
"start_time" = "2014-11-11 19:00:00";
"time_slot" = "7:00 PM";
},
{
"appointment_id" = 246;
"component_name" = p1;
id = 42;
"start_time" = "2014-11-11 18:00:00";
"time_slot" = "6:00 PM";
},
{
"appointment_id" = 246;
"component_name" = p2;
id = 41;
"start_time" = "2014-11-11 18:00:00";
"time_slot" = "6:00 PM";
},
{
"appointment_id" = 246;
"component_name" = p3;
id = 43;
"start_time" = "2014-11-11 18:00:00";
"time_slot" = "6:00 PM";
}, 
{
"appointment_id" = 247;
"component_name" = p4;
id = 44;
"start_time" = "2014-11-11 19:00:00";
"time_slot" = "7:00 PM";
},
{
"appointment_id" = 247;
"component_name" = p5;
id = 45;
"start_time" = "2014-11-11 19:00:00";
"time_slot" = "7:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r1;
id = 29;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r10;
id = 38;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r11;
id = 39;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r12;
id = 40;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r2;
id = 30;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r3;
id = 31;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r4;
id = 32;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r5;
id = 33;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r6;
id = 34;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r7;
id = 35;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r8;
id = 36;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r9;
id = 37;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
}
)

使用Onik IV方法排序后,这就是我要注意的“ time_slot” =“ 7:00 PM” ;:

<__NSArrayM 0x17e9b660>(
<__NSArrayM 0x19088e80>(
{
"appointment_id" = 244;
"component_name" = "badge test";
id = 20;
"start_time" = "2014-11-11 19:00:00";
"time_slot" = "7:00 PM";
}
)
,
<__NSArrayM 0x190543b0>(
{
"appointment_id" = 246;
"component_name" = p1;
id = 42;
"start_time" = "2014-11-11 18:00:00";
"time_slot" = "6:00 PM";
},
{
"appointment_id" = 246;
"component_name" = p2;
id = 41;
"start_time" = "2014-11-11 18:00:00";
"time_slot" = "6:00 PM";
},
{
"appointment_id" = 246;
"component_name" = p3;
id = 43;
"start_time" = "2014-11-11 18:00:00";
"time_slot" = "6:00 PM";
}
)
,
<__NSArrayM 0x1905d750>
(
{
"appointment_id" = 247;
"component_name" = p4;
id = 44;
"start_time" = "2014-11-11 19:00:00";
"time_slot" = "7:00 PM";
},
{
"appointment_id" = 247;
"component_name" = p5;
id = 45;
"start_time" = "2014-11-11 19:00:00";
"time_slot" = "7:00 PM";
}
)
,
<__NSArrayM 0x190243e0>
(
{
"appointment_id" = 245;
"component_name" = r1;
id = 29;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r10;
id = 38;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r11;
id = 39;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r12;
id = 40;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r2;
id = 30;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r3;
id = 31;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r4;
id = 32;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r5;
id = 33;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r6;
id = 34;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r7;
id = 35;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r8;
id = 36;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r9;
id = 37;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";

})

如果您看到“ time_slot” =“ 7:00 PM”;

它不在其他“ time_slot” =“ 7:00 PM”的同一数组中分组;被分组。

希望我能说得更清楚。谢谢并恭祝安康。

奥尼克四世

这是一种方法:

NSArray *arrAllocated; // Your initial array
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"time_slot" ascending:YES];
NSArray *sorts = @[sort];
NSArray *orderedArrAllocated = [arrAllocated sortedArrayUsingDescriptors:sorts];

NSMutableArray *groupedFilterArray = [[NSMutableArray alloc] init];
NSMutableArray *currentMutableArray;
NSDictionary *lastObject;

for (NSDictionary *dict in orderedArrAllocated)
{
    if ([[dict valueForKey:@"time_slot"] isEqualToString:[lastObject valueForKey:@"time_slot"]])
    {
        [currentMutableArray addObject:dict];
    }
    else
    {
        NSMutableArray *newMutableArray = [[NSMutableArray alloc] initWithObjects:dict, nil];
        lastObject = dict;
        currentMutableArray = newMutableArray;
        [groupedFilterArray addObject:newMutableArray];

    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档