Created
April 11, 2011 14:57
-
-
Save shinypb/913649 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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