我想排序一年中充满月份的数组,例如:
["Nov 2019","Apr 2020","MAR 2018","Jan 2018","May 2018"]
(非分类)
到:
["Jan 2018","Mar 2018","May 2018","Nov 2019","Apr 2020"]
(已排序)像这样...
func sortDate( arr : inout [String]) {
let months : [String] = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
for i in 0..<arr.count{
for j in i..<arr.count{
let string1 = arr[i]
let string2 = arr[j]
let month1 = String(string1[string1.startIndex...string1.index(string1.startIndex, offsetBy: 2)])
let month2 = String(string2[string2.startIndex...string2.index(string2.startIndex, offsetBy: 2)])
if months.index(of: month1.lowercased())! > months.index(of: month2.lowercased())! {
let temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
}
}
for i in 0..<arr.count{
for j in i..<arr.count{
let string1 = arr[i]
let string2 = arr[j]
if (string1[string1.index(before: string1.endIndex)..<string1.endIndex] as NSString).intValue < (string2[string2.index(before: string2.endIndex)..<string2.endIndex] as NSString).intValue {
let temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
}
}
}
这是我尝试的但是它无法正常工作。
这是输入:
["Jan 2020", "Feb 2018", "Mar 2018", "Apr 2018", "May 2018", "Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018", "Jun 2019", "Feb 2019", "Jul 2019", "Apr 2019", "Aug 2019", "Sep 2019", "May 2019", "Oct 2019", "Jan 2019", "Mar 2019", "Nov 2019"]
输出是:
["Jan 2019", "Feb 2019", "Mar 2019", "Apr 2019", "May 2019", "Jun 2019", "Jul 2019", "Aug 2019", "Sep 2019", "Oct 2019", "Nov 2019", "Jul 2018", "Mar 2018", "May 2018", "Aug 2018", "Feb 2018", "Sep 2018", "Oct 2018", "Apr 2018", "Nov 2018", "Jun 2018", "Jan 2020"]
答案 0 :(得分:2)
我可能会使用日期格式化程序来解析这些日期,然后按以下顺序排序:
let input = ["Jan 2020", "Feb 2018", "Mar 2018", "Apr 2018", "May 2018", "Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018", "Jun 2019", "Feb 2019", "Jul 2019", "Apr 2019", "Aug 2019", "Sep 2019", "May 2019", "Oct 2019", "Jan 2019", "Mar 2019", "Nov 2019"]
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "MMM yyyy"
let output = input.sorted { string1, string2 in
guard let date1 = formatter.date(from: string1) else { return true }
guard let date2 = formatter.date(from: string2) else { return false }
return date1 < date2
}
或者,如果你有很多字符串,并且想要消除多余的格式化程序转换:
let output = input
.map { (string: $0, date: formatter.date(from: $0)) }
.sorted {
guard let date1 = $0.date else { return true }
guard let date2 = $1.date else { return false }
return date1 < date2
}.map { $0.string }
答案 1 :(得分:1)
试试这个:
let dateStringArray = ["Jan 2020", "Feb 2018", "Mar 2018", "Apr 2018", "May 2018", "Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018", "Jun 2019", "Feb 2019", "Jul 2019", "Apr 2019", "Aug 2019", "Sep 2019", "May 2019", "Oct 2019", "Jan 2019", "Mar 2019", "Nov 2019"]
var dateArray = [Date]()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM yyyy"
for dateString in dateStringArray{
let date = dateFormatter.date(from: dateString)
if let convertedDate = date{
dateArray.append(convertedDate)
}
}
for date in dateArray.sorted(by: {$0 < $1}){
let dateString = dateFormatter.string(from: date)
print(dateString)
}
您将获得可以存储的日期字符串,并且将对字符串数组进行排序。
答案 2 :(得分:0)
Hello @Boobesh请使用此代码。
let sortedArray = datesArray.sorted { ($0 as! String).localizedCaseInsensitiveCompare($1 as! String) == .orderedDescending}