I need to force a specific order into the resulting Event Status
array but it's a bit beyond my experience. How would I force the following order into the sections
array:
- Live
- Active
- Completed
- Archived
Currently the are appended into any random order of the options above
//Retrieving list of Event Status from event documents
let groups = Dictionary(grouping: self.retrievedEvents) { (event) -> String in
return event.eventStatus
}
//Grouping events in arrays based on the event status of the event
self.sections = groups.map { (eventStatus, events) in
EventStatus(eventStatus: eventStatus, events: events)
}
Current results for group:
["active": [Gallery.Event, Gallery.Event], "archived": [Gallery.Event, Gallery.Event, Gallery.Event, Gallery.Event], "live": [Gallery.Event, Gallery.Event], "completed": [Gallery.Event]]
Current results for sections:
[Gallery.EventStatus(eventStatus: Optional("active"), events: [Gallery.Event, Gallery.Event]), Gallery.EventStatus(eventStatus: Optional("archived"), events: [Gallery.Event, Gallery.Event, Gallery.Event, Gallery.Event]), Gallery.EventStatus(eventStatus: Optional("live"), events: [Gallery.Event, Gallery.Event])]
Event Model (Updated)
class Event {
var adminUser = ""
var byId = ""
var eventCreated:Timestamp?
var eventId = ""
var eventName = ""
var eventStart = ""
var eventStatus: EventStatusTypes = .active
}
Updated model with enum:
enum EventStatusTypes: String, CaseIterable, Comparable {
case live
case completed
case active
case created
case archived
static func < (lhs: EventStatusTypes, rhs: EventStatusTypes) -> Bool {
return allCases.firstIndex(of: lhs)! < allCases.firstIndex(of: rhs)!
}
}
'
struct EventStatus {
var eventStatus = ""
var events: [Event]
}
'
Current having issues with eventStatus:
init?(snapshot:DocumentSnapshot){
self.eventId = snapshot.get("eventId") as? String ?? "No event Id"
self.byId = snapshot.get("byId") as? String ?? "No uid"
self.adminUser = snapshot.get("adminUser") as? String ?? "No admin user"
**self.eventStatus = snapshot.get(eventStatus.rawValue) as? EventStatusTypes ?? EventStatusTypes(rawValue: EventStatusTypes.active.rawValue)!**
}