如何对工作日字符串数组进行排序?

时间:2016-03-17 14:56:35

标签: swift nsdateformatter

我从:

开始
var weekdays = ["Sunday", "Friday", "Tuesday"]

我需要将其更改为:

weekdays = ["Sunday", "Tuesday", "Friday"]

我已将它们转换为Ints:

var days = [Int]()

for day in weekdays {

    let formatterWeekday = NSDateFormatter()

    formatterWeekday.dateFormat = "e"

    let weekday = formatterWeekday.dateFromString(day)

    let weekdayString = formatterWeekday.stringFromDate(weekday!)

    let dayInt = Int(weekdayString)

    days.append(dayInt!)

}

days.sortInPlace()

然后给了我:

days = [1,3,6]

那么如何将Int转换为工作日字符串?

1 个答案:

答案 0 :(得分:4)

您是否考虑过简单方法?

更新了@Martin R输入

let dateFormatter = NSDateFormatter() 
// dateFormatter.weekdaySymbols = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]


for dayIndex in days
{    
    print(dateFormatter.weekdaySymbols[dayIndex])
}