UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Changing order of array

Forums > Swift

I have an array of string but would like to change the order that they show depending on user input.

//Array of day
let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
// User input
let startDayOfWeek = 1

var startOfWeek: [String] {
    guard startDayOfWeek != 0 else { return days }
    var newDays = [String]()

    for i in startDayOfWeek..<days.count {
        newDays.append(days[i])
    }

    for i in 0..<startDayOfWeek {
        newDays.append(days[i])
    }

    return newDays
}

print(startOfWeek)

Is there a better way to do this?

2      

maybe as simple as this...

enum StartOfWeek {
    case sun
    case mon
}

var startOfWeek = StartOfWeek.mon
var weekDays: [String] = []

switch startOfWeek {
case .sun:
    weekDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
case .mon:
    weekDays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
}

or like this

enum StartOfWeek {
    case sun
    case mon
}

var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

var startOfWeek = StartOfWeek.sun

switch startOfWeek {
case .sun:
    days
case .mon:
    days = Array(days.dropFirst())
    days.append("Sun")
}

or that )))

enum StartOfWeek {
    case sun
    case mon
}

var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

var startOfWeek = StartOfWeek.mon

switch startOfWeek {
case .sun:
    days
case .mon:
    let day = days.removeFirst()
    days.append(day)
}

but I think this is more readable )))

enum StartOfWeek {
    case sun
    case mon
}

let sunday = "Sun"
var days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

var startOfWeek = StartOfWeek.sun

switch startOfWeek {
case .sun:
    days.insert(sunday, at: 0)
case .mon:
    days.append(sunday)
}

2      

hi,

i took Nigel's question to be a more general one of rotation, not just whether the startOfWeek is 0 or 1. e.g., a business might be closed on Sunday and Monday, so people working there might treat their weekdays as Tuesday through Saturday.

this would work:

var startOfWeek: [String] {
    let startIndex = max(startDayOfWeek, 0) % 7 // startIndex >= 0 and values 7 + higher ==> start counting again at "Sun"
    let daysStartingAt = days.dropFirst(startIndex) + days.dropLast(7 - startIndex)
    return(Array(daysStartingAt))
}

you can also find a rotate function defined on BidirectionalCollection in the Algorithms package. that would look like this

import Algorithms

// other stuff ...

var startOfWeek: [String] {
    let startIndex = max(startDayOfWeek, 0) % 7 // values 7 + higher ==> start counting again at "Sun"
    var normalDays = days // makes a copy
    normalDays.rotate(toStartAt: startIndex)// rotates in place
    return normalDays
}

hope that's of interest,

DMG

2      

Thank you for your replys. Yes it a calendar and would like the option to pick the start day of week (this can be any day not just Sunday or Monday). I choose Int as when working out all the days from a selected Date it will then work out the first weekday of month, using

let firstDayOfWeek = calendar.component(.weekday, from: startOfMonth)

So firstDayOfWeek match the order in the array however if I change the order then will need to adjust the startDayOfWeek.

If you want to see that code I can show it to you.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.