Skip to content

Instantly share code, notes, and snippets.

@shinypb
Created April 11, 2011 14:57
Show Gist options
  • Select an option

  • Save shinypb/913649 to your computer and use it in GitHub Desktop.

Select an option

Save shinypb/913649 to your computer and use it in GitHub Desktop.
NSDictionary groupItems(NSArray *items) {
int bucket = 0;
NSDictionary *groupedItems = [NSDictionary alloc];
id prevItem;
const kItemTimeDistanceThreshold = 30;
[groupedItems setObject:[NSArray alloc] forKey:bucket];
// I can't remember how to properly iterate in Objective C,
// so let's pretend you can use foreach.
foreach(item in items) {
if(prevItem && [item time] - [prevItem time] >= kItemTimeDistanceThreshold) {
// Start putting objects into the next bucket
bucket++;
[groupedItems setObject:[NSArray alloc] forKey:bucket];
}
[[groupedItems objectForKey:bucket] addObject:item];
prevItem = items;
}
return groupedItems;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment