2014-11-04 110 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

相關問題