I have an NSArray of Object that has an interesting property that I would like to use in the following way: Given my array of objects with properties:
Object1 - Property A;
Object2 - Property A;
Object3 - Property B;
Object4 - Property D;
Object5 - Property D;
Object6 - Property D
I want these to be bucket sorted by their properties into a new array:
Array1 - Objects Object1, Object2
Array2 - Objects Object3
Array3 - Objects Object 4, Object5, Object6
And then within each array, sort by using a timeStamp property.
I have tried to accomplish this naively by creating a dictionary, adding interesting objects to the dictionary by property like if ([dictionary objectForKey:@"propertyVal"]) //add object else // create array for key, add object to array. This approach has not worked as expected because I end up needing to dekey the NSMutableDictionary using allKeysForValue, which is not reliable.
I feel that this is a fairly common problem and I would love to hear any insight into how I might go about solving this. Code is great, but even an algorithm (with the appropriate objects to use) should suffice.
It’s not a proper bucket sort, but should work for a set of three properties. A bit of fiddling and you should be able to adjust it for any number of properties:
Edit. I made a dynamic version (just set property type to what you need):