如何排序字符串的日期?

时间:2017-06-06 08:24:43

标签: ios arrays swift sorting

我有一个日期数组2017-07-06,2017-06-07,2017-07-07。我想按升序排序这些日期。所以2017-06-07将是第一位的。但它根本没有发生。我使用下面的代码:

func sortDates(array:NSMutableArray)->[String]  {

        var strArray:[String] = []
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"// yyyy-MM-dd"
        var convertedArray: [Date] = []
        for dat in array {
            let date = dateFormatter.date(from: dat as! String)
            convertedArray.append(date!)
        }
        let arrDates = convertedArray.sorted(by: { $0.compare($1) == .orderedAscending })
        for value in arrDates {
            let formatter = DateFormatter()
            // initially set the format based on your datepicker date
            formatter.dateFormat = "yyyy-MM-dd"
            let myString = formatter.string(from: value)
            // convert your string to date
            let yourDate = formatter.date(from: myString)
            if yourDate != nil {
                //then again set the date format whhich type of output you need
                formatter.dateFormat = "yyyy-MM-dd"
                // again convert your date to string
                let myStringafd = formatter.string(from: yourDate!)
                strArray.append(myStringafd)
            }
        }
        return strArray
    }

请指导我在哪里做错了?

4 个答案:

答案 0 :(得分:1)

您可以创建一个扩展程序,将string转换为date,然后将所有dates-objects追加到array并对array进行排序。< / p>

扩展:

extension String {
    var toDate: Date {
        return Date.Formatter.customDate.date(from: self)!
    }
}

extension Date {
    struct Formatter {
        static let customDate: DateFormatter = {
            let formatter = DateFormatter()
            formatter.locale = Locale(identifier: "en_US_POSIX")
            formatter.dateFormat = "yyyy-MM-dd"
            return formatter
        }()
    }
}

用法:

let d1 = "2017-01-01".toDate
let d2 = "2016-01-01".toDate
let d3 = "2018-01-01".toDate
let d4 = "2013-01-01".toDate

let convertedArray = [d1,d2, d3, d4]
let sorted = convertedArray.sorted() // note that the sort works on a string too.
print(sorted)

答案 1 :(得分:1)

没有必要将这些字符串转换为Date类型。您可以简单地将它们排序为字符串,它们将以正确的顺序出现。

答案 2 :(得分:0)

extension String  {

func date(format: String) -> Date? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = format
    let date = dateFormatter.date(from: self)
    return date
}

}
extension Date {
func string(format: String) -> String {
    let dateFormatterPrint = DateFormatter()
    dateFormatterPrint.dateFormat = format
    return dateFormatterPrint.string(from: self) as String
}
}
var datesSting = ["2017-07-06","2017-08-07","2017-07-07"]

let soteddates  = datesSting.map { (obj) -> Date in
    return obj.date(format: "yyyy-MM-DD")!
}

let sorteedStings = soteddates.sorted { (obj1, obj2) -> Bool in
    return obj1.compare(obj2) == ComparisonResult.orderedAscending
}.map { (obj) -> String in
   return obj.string(format:  "yyyy-MM-DD")
}

print(sorteedStings)

答案 3 :(得分:0)

这是我的代码:

   let arrCurrentDate = [“2017-07-06”, “2017-06-07", “2017-07-07”]
   var arrConverted: [Date] = []

   let dateFrmt = DateFormatter()
   dateFrmt.dateFormat = “yyyy-MM-dd”

   for dateCurrent in arrCurrentDate {
     let date = dateFrmt.date(from: dateCurrent)
     arrConverted.append(date!)
   }       
   arrConverted.sort(by: {$0.compare($1) == .orderedAscending})       
   arrConverted.sorted(by: {$0.timeIntervalSince1970 < $1.timeIntervalSince1970})

   print(arrConverted)

您也可以更改订单:

<强> .orderedAscending .orderedSame .orderedDescending