0

I have an array of objects which I am currently sorting by date (the objects each have a date field on them).

I want to be able to split up this array into multiple sections, where each section contains the objects that have the same date. Is there already a supported mechanism for this?

2
  • Yes just implement UITableViewDataSource's numberOfSectionsInTableView and numberOfRowsInSection. Commented Mar 20, 2014 at 23:34
  • I need to be able to first figure out how many sections I need and how many rows in each one...That is the entire point of building this data struct. Commented Mar 20, 2014 at 23:38

2 Answers 2

1

There seems to be no mechanism for this, other than the way I suggested in my comment:

I create a nsmutabledict, then I iterate over my sorted list of objects. For each object, I check the date, and see if there exists a key in my dict pertaining to that date. If so, I retrieve that value from the dict and append this object to that array. If not, I create a new list and insert it into the dict with the key pertaining to the date.

Sign up to request clarification or add additional context in comments.

Comments

0
- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate

will be useful. Write a predicate to get the filtered array of all the elements matching the date of the first item in the array. Add that array to a new mutable array, which is the object holding all the filtered arrays with matching dates.

Record that date as having been used. Iterate over the original array until you find a different date and repeat the filteredArrayUsingPredicate operation. Keep doing this until you have covered all unique dates in the array.

I was going to suggest using NSSet to remove all the items that have been filtered already to reduce processing time but the number of items may be important, and depending on the objects NSSet could discard duplicates.

An optimisation could be to use NSSet to non-destructively create a set of unique dates from the original array and then iterate over that set, performing the filteredArrayUsingPredicate operation. But whether that is practical depends on your data.

6 Comments

This seems like a pretty inefficient solution. I will be traversing the entire array once for each unique date that is present. I can simply do it in one traversal manually (create nsmutabledict to track dates, traverse sorted array, get date, see if date key exists in dict. If it does, append to the end of that key's value, else create a new dict key with a nsmutablearray as its value.
Sure, but without knowing about your data - plain NSDate or object containing NSDate? it is hard to suggest further.
It is an object containing an NSDate
Yes, I can get it done. I was just wondering if there was already a mechanism in place -- similar to how I don't have to implement my own sorting algo. If not, I can just use the method I mentioned.
@AtulBhatia, no, there is no canned way to do that. The way you are doing it is as good a way as any.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.