Pregunta

I have a NSMuttableArray having values like

(
{
    MedicineAlarmTime = "18:00:00";
    MedicineDateTime = "24/04/2014 16:00:09";
},
{
    MedicineAlarmTime = "18:00:00";
    MedicineDateTime = "24/04/2014 16:00:26";
},
{
    MedicineAlarmTime = "19:00:00";
    MedicineDateTime = "24/04/2014 16:00:26";
},
{
    MedicineAlarmTime = "19:00:00";
    MedicineDateTime = "24/04/2014 16:00:26";
}
)

Is it possible to retrieve arrays of similar "MedicineAlarmTime" like

array1 = (
{
    MedicineAlarmTime = "18:00:00";
    MedicineDateTime = "24/04/2014 16:00:09";
},
{
    MedicineAlarmTime = "18:00:00";
    MedicineDateTime = "24/04/2014 16:00:26";
}
)

array2 = (
{
    MedicineAlarmTime = "19:00:00"
    MedicineDateTime = "24/04/2014 16:00:09";
},
{
    MedicineAlarmTime = "19:00:00";
    MedicineDateTime = "24/04/2014 16:00:26";
}
)
¿Fue útil?

Solución

NSMutableDictionary *groupedAlarms = [NSMutableDictionary dictionary];

for (NSDictionary *alarm in originalArray) {
    NSString *alarmTime = [alarm objectForKey: MedicineAlarmTime];

    NSMutableArray *alarmGroup = [groupedAlarms objectForKey: MedicineAlarmTime]
    if (alarmGroup) {
        [alarmGroup addObject: alarm];
    } else {
        alarmGroup = [NSMutableArray arrayWithObjects: alarm, nil];
        [groupedAlarms setValue:alarmGroup forKey:alarmTime];
    }
}

This creates an NSMutableDictionary, groupedAlarms. The keys in groupedAlarms are the alarm times from the dictionaries in the original array--the actual alarm time value. The values for each key are arrays. The arrays contain the dictionaries of the original array--sorted based on the alarm time (which is used as the key to the array which contains them).


If you want the "alarmGroup" from the above example to be more general and not group exact matches only, you can use the same forin loop structure. The checks to determine whether to put it in an existing group or create a new group become slightly more complex, but the important thing is, we still don't need to presort the array and then iterate through it a second time.

The only reason I can see for pre-sorting would be if you had a set number of groups you wanted, and a set number of objects in each group. Then you can't really know what group to put each object in until the array has been sorted.

But here, we're trying to put objects in groups with similar object, so the algorithm is simple:

Grab an object. See if it belongs in an existing group. If not, create a new group for this object. Move to the next object.

And for any downvoters... I can't really provide example code for a more complex example than the existing sample code in this answer unless the original question is made more clear.

Otros consejos

Sure. You just have to write code for it.

First, you use the NSArray method sortedArrayUsingDescriptor: to get an array that is sorted by MedicineAlarmTime, so array elements that you might want together will actually be together. Then you iterate through the array, comparing the MedicineAlarmTime to check if two array elements are "similar" enough for you, and create arrays with those groups, using the NSArray method subArrayWithRange:

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top