0

In my App I have an array of objects 'class: EventObjects' with several properties like 'date: NSDate?' and 'stuffToDo: String?' that are fetched from a calendar database. What I try to achieve now is putting all EventObjects with the same date property together in another object of 'class: EventsAtSameDate'.

class EventsAtSameDate: NSObject
{
   var date:NSDate?
   var eventObjects:NSArray<EventObject>?
}

And finally have a nested array with the EventsAtSameDate.

var nestedArrayOfEventsAtSameDateObjects: Array<EventsAtSameDate>?

I know how to search and sort an array with .filter .sort .sortInPlace etc. functions. But how can I compare the dates in the array with each other and put them in another nested Array?

2 Answers 2

2

You could instantiate a dictionary to keep track of the dates present while doing one iteration over the initial array of type EventObject. Then iterate the the dictionary to instantiate classes of EventsAtSameDate and append them to the nestedArrayOfEventsAtSameDateObjects: [EventsAtSameDate]

The code would look something like:

var nestedArrayOfEventsAtSameDateObjects = [EventsAtSameDate]()

//instantiate a dictionary that uses Date as the key and an array of EventObject as the associated value
var dictionary = [Date: [EventObject]]()

//iterate through your intial array of EventObjects
for obj in eObjs {
    //check if you have already seen the date in the collection
    if dictionary[obj.date] != nil {
        //if you have - then append it to that date
        dictionary[obj.date]?.append(obj)
        continue
    }
    //otherwise, add it to the dictionary for a check later in the loop
    dictionary[obj.date] = [obj]
}


//then iterate through the dictionary

for (date, eObjs) in dictionary {
    if eObjs.count > 1 {
        let sameDate = EventsAtSameDate()
        sameDate.date = date
        sameDate.eventObjects = eObjs
        nestedArrayOfEventsAtSameDateObjects.append(sameDate)
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

1

I would sort the array of your events by date and then iterate over them. Check if the date is newer then the previous one and create another storage if so.

class Event {
    var date:Date!
}

class EventsList
{
    var date:Date!
    var events:[Event]!
}

func createEventsLists(sortedEvents: [Event]) -> [EventsList] {
    guard sortedEvents.count > 0 else { return [] }

    var currentEventList = EventsList()
    var result = [currentEventList]
    var lastDate = sortedEvents.first!.date

    for event in sortedEvents {
        let newDate = event.date.compare(lastDate!) != .orderedDescending
        if newDate {
            currentEventList = EventsList()
            result.append(currentEventList)
        }
        currentEventList.events.append(event)
        lastDate = event.date
    }
    return result
}

1 Comment

That could also work. In my case the hint with the Dictionary from syllabix solved my problem. Thanks.

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.