2014-11-04 76 views
1

匹配我有一个NSMutable arrray含NSDictionaries这样的:如何获得一个数组(说arrayGroupBy)字典从字典中的数组,其中一些关键的价值在另一个字典键值

<__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”;被分组。

希望我能够使它更清楚。 感谢和问候。

+0

你有什么尝试吗? – 2014-11-04 14:13:46

+0

是的,我已经尝试过使用谓词,请参阅编辑 – Alok 2014-11-04 14:16:06

+1

它是否必须是字典?你不能使用自定义对象吗? – Fogmeister 2014-11-11 10:27:10

回答

2

这里有一个办法:

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]; 

    } 
} 
+0

感谢您的答案,它的一些字典,而不是其他人 – Alok 2014-11-11 10:08:56

+1

@Alok请解释什么不在这里工作。当你说它不分组别人。你到底什么意思?哪些人?我正要写一个答案,并意识到它和这完全一样。 – Fogmeister 2014-11-11 10:33:03

+0

@Fogmeister让我调试并为您提供日志 – Alok 2014-11-11 10:35:03

3

我用的是这样的:

NSArray *yourArray = /* Initialize */; 
// Get all time slots 
NSArray *allTimeSlotsArray = [yourArray valueForKey:@"time_slot"]; 
// Remove duplucates 
NSSet *allTimeSlotsSet = [NSSet setWithArray:allTimeSlotsArray]; 
    // Enumerate 
NSMutableDictionary *recordsByTimeSlot = [NSMutableDictionary dictionary]; 
for (NSString *timeSlot in allTimeSlotsSet) { 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"time_slot == %@", timeSlot]; 
    NSArray *matchingArray = [yourArray filteredArrayUsingPredicate:predicate]; 
    recordsByTimeSlot[timeSlot] = matchingArray; 
} 

更新:
商店matchingArray或做任何你需要的。
使用生成的recordsByTimeSlot字典。

2

@orkenstein答案在这里完美的作品。但唯一的问题是如果你不保存匹配数组,它将被覆盖。为此,您可以使用NSMutableArray并在for循环中添加匹配的数组。我没有足够的声望将其作为评论发布,因此我将其作为答案发布。 :)

-(void)filteredArray 
{ 
     NSArray *yourArray = originalArray; 
     // Get all time slots 
     NSArray *allTimeSlotsArray = [yourArray valueForKey:@"time_slot"]; 
     // Remove duplucates 
     NSSet *allTimeSlotsSet = [NSSet setWithArray:allTimeSlotsArray]; 
     // Enumerate 
    for (NSString *timeSlot in allTimeSlotsSet) { 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"time_slot == %@", timeSlot]; 
     NSArray *matchingArray = [yourArray filteredArrayUsingPredicate:predicate]; 
     [finalArray addObjectsFromArray:matchingArray]; 
    } 
    NSLog(@"Filtered Array : %@",finalArray); 
} 

finalArray是一个NSMutableArray对象。

+0

我认为这太明显了) – orkenstein 2014-11-18 06:43:20

相关问题