Thoms
Thoms

Reputation: 311

Compare objects within Array and put equal values in seperate new object

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?

Upvotes: 0

Views: 86

Answers (2)

alexburtnik
alexburtnik

Reputation: 7741

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
}

Upvotes: 1

syllabix
syllabix

Reputation: 2295

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)
    }
} 

Upvotes: 2

Related Questions