0

I have an array value that needs to be in this order

let daysOfWeekArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

and currently my array looks like this

let allData = [CustomObject(day:"Tuesday", startTime: "5:00"), CustomObject(day:"Sunday", startTime: "3:00")]
array.append(allData)

How do I get the array to sort by the day?

Sorry if this seems like an easy question, new to sorting arrays.

3 Answers 3

2

You can look up the index of the day in your reference array and sort by that index value

array.sort(by: { 
    (daysOfWeekArray.firstIndex(of: $0.day) ?? Int.max) < (daysOfWeekArray.firstIndex(of: $1.day) ?? Int.max)})

I use Int.max in case the day isn't found so that element gets sorted last.

On a side note, the array you are using for order can be gotten from the Calendar class from the property weekDaySymbols so the sorting could be done by using that property directly

let calendar = Calendar.current
array.sort(by: { 
    (calendar.weekdaySymbols.firstIndex(of: $0.day) ?? Int.max) < (calendar.weekdaySymbols.firstIndex(of: $1.day)  ?? Int.max)})

Of course you need to be sure you use the right locale for your calendar.

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

1 Comment

Sort is O(nLog(n)) and first index is O(n) so this whole thing is O(n^2)*log(n) which is pretty disasterous performance wise; fortunately there aren't many days of the week, but really you should use a dictionary instead of an array.
0

Well maybe not the best solution, but looks like worked.

let daysOfWeekArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

struct CustomObject {
    var day: String
    var startTime: String
}

let allData = [CustomObject(day:"Tuesday", startTime: "5:00"), CustomObject(day:"Friday", startTime: "3:00"), CustomObject(day:"Sunday", startTime: "3:00"), CustomObject(day:"Monday", startTime: "3:00")]

let sorted = allData.sorted { (a, b) -> Bool in
    let aIndex = daysOfWeekArray.firstIndex(of: a.day)!
    let bIndex = daysOfWeekArray.firstIndex(of: b.day)!
    if aIndex > bIndex {
        return false
    } else {
        return true
    }
}
print(sorted)

Comments

0

If I were you, I would change those strings to enum like that:

enum DayOfWeek {
    case sunday
    case monday
    case tuesday
    case wednesday
    case thursday
    case friday
    case saturday

    var title: String {
        switch self {
        case .sunday:       return "Sunday"
        case .monday:       return "Monday"
        case .tuesday:      return "Tuesday"
        case .wednesday:    return "Wednesday"
        case .thursday:     return "Thursday"
        case .friday:       return "Friday"
        case .saturday:     return "Saturday"
        }
    }

    var dayInWeek: Int {
        switch self {
        case .sunday:       return 0
        case .monday:       return 1
        case .tuesday:      return 2
        case .wednesday:    return 3
        case .thursday:     return 4
        case .friday:       return 5
        case .saturday:     return 6
        }
   }

    static var all: [DayOfWeek] {
       return [.sunday, .monday, .tuesday, .wednesday, .thursday, .friday, .saturday] 
    }
}
struct CustomObject {
    let day: DayOfWeek
    let startTime: String
    // ....
}

let daysOfWeekArray = DayOfWeek.all

let allData = [CustomObject(day:.tuesday, startTime: "5:00"), CustomObject(day:.sunday, startTime: "3:00")]
var array: [CustomObject] = []
array.append(contentsOf: allData)
array = array.sorted { $0.day.dayInWeek < $1.day.dayInWeek }
print(array.map{$0.day.title})

Comments

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.