在Swift 4中从最新到最旧的日期排序数组

时间:2018-12-11 11:48:38

标签: ios arrays date

我很好奇如何在Strings中以array的形式对日期进行排序。我很可能会将它们放在相同的arraysort中,并放置在正确的位置。我见过的许多尝试都是两次。另外,它们也不是正确的时间格式,因此我想知道如何使用"dd.MM.yyyy"格式对其进行排序。很可能只有一年的最后两个数字。有可能吗?

这就是我所做的。我希望它采用结构中的日期进行排序:

import UIKit

struct Post {
var title: String
var date: String
var user: String
}

var uploadTimes = [Post(title: "Hello", date: "02.10.2018", user: "Roman"),
                Post(title: "Hi", date: "01.10.2018", user: "Roman"),
                Post(title: "", date: "05.09.2018", user: "")]


uploadTimes = uploadTimes.map{ $0.components(separatedBy: ".").reversed().joined(separator: ".")}
print(uploadTimes)
//["2002.02.02", "2002.02.03", "2002.02.05", "2002.02.04"]


let isoFormatter = ISO8601DateFormatter()
isoFormatter.formatOptions = [.withFullDate,]


let dates = uploadTimes.compactMap { isoFormatter.date(from: $0) }
print(dates)
let sortedDates = dates.sorted { $0 > $1 }
print(sortedDates)

3 个答案:

答案 0 :(得分:5)

您不能为ISO8601DateFormatter指定日期格式,它仅接受标准ISO格式的日期。

如果将其替换为普通的DateFormatter(),则可以指定日期格式。参见下面的代码:

var uploadTimes = ["02.02.2002","03.02.2002","05.02.2002","04.02.2002"]

print(uploadTimes)

let isoFormatter = DateFormatter()
isoFormatter.dateFormat = "dd.MM.yyyy"

let dates = uploadTimes.compactMap { isoFormatter.date(from: $0) }

let sortedDates = dates.sorted { $0 > $1 }

print(sortedDates)

let dateStrings = sortedDates.compactMap { isoFormatter.string(from: $0)}

print(dateStrings)

答案 1 :(得分:2)

这是另一个具有更新的POST结构的答案。

struct Post {
var title: String
var date: String
var user: String
}

extension Post{
static  let isoFormatter : ISO8601DateFormatter = {
    let formatter =  ISO8601DateFormatter()
        formatter.formatOptions = [.withFullDate,]
    return formatter
   }()

var dateFromString : Date  {
    let  iSO8601DateString = date.components(separatedBy: ".").reversed().joined(separator: ".")
    return  Post.isoFormatter.date(from: iSO8601DateString)!
}
}

var uploadTimes = [Post(title: "", date: "05.09.2018", user: ""),
               Post(title: "Hello", date: "02.10.2018", user: "Roman"),
               Post(title: "Hi", date: "01.10.2018", user: "Roman"),
               ]

//sort dates
let dates = uploadTimes.compactMap { $0.dateFromString }
print(dates)
let sortedDates = dates.sorted { $0 > $1 }
print(sortedDates)

//sort posts
let sortedPost = uploadTimes.sorted{ $0.dateFromString > $1.dateFromString  }
print(sortedPost)

如果格式为hh:mm dd.MM.yyyy。更新后的答案是这样的:

extension Post{
static  let isoFormatter : ISO8601DateFormatter = {
    let formatter =  ISO8601DateFormatter()
        formatter.formatOptions = [.withInternetDateTime]
    return formatter
   }()

   var dateFromString : Date  {

  let components = date.components(separatedBy: " ")
  let  iSO8601DateString =
        (components.last!).components(separatedBy: ".").reversed().joined(separator: ".") + "T" + (components.first!) + ":00+00:00"
    return  Post.isoFormatter.date(from: iSO8601DateString)!
   }
}

 var uploadTimes = [Post(title: "", date: "01:20 05.09.2018", user: ""),
               Post(title: "Hello", date: "01:21 02.10.2018", user: "Roman"),
               Post(title: "Hi", date: "01:22 02.10.2018", user: "Roman"),
               ]

答案 2 :(得分:0)

如果您坚持使用ISODateFormatter,则可能必须更改输入格式,因为它们采用的格式是“ yyyy.MM.dd”。

 import UIKit

 var uploadTimes = ["02.02.2002","03.02.2002","05.02.2002","04.02.2002"]
 uploadTimes = uploadTimes.map{ $0.components(separatedBy: ".").reversed().joined(separator: ".")}
 print(uploadTimes)
  //["2002.02.02", "2002.02.03", "2002.02.05", "2002.02.04"]


 let isoFormatter = ISO8601DateFormatter()
 isoFormatter.formatOptions = [.withFullDate,]


 let dates = uploadTimes.compactMap { isoFormatter.date(from: $0) }
 print(dates)
 let sortedDates = dates.sorted { $0 > $1 }
 print(sortedDates)