Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting array of date from newest to oldest in Swift 4

Tags:

arrays

date

ios

I am curious how to sort dates in form of Strings in an array. Most likely I would leave them in the same array and sort them the right way. Many attempts I have seen are with two. Also, they weren't the right time format so I want to know how to sort it with the "dd.MM.yyyy" format. Most likely with only the last two numbers of the year. Is that possible?

Here is what I did. I want it to take the date in the struct to sort:

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)
like image 844
Thomas22 Avatar asked Dec 07 '22 13:12

Thomas22


2 Answers

You can't specify the date format for an ISO8601DateFormatter, it only accepts dates in the normal ISO format.

If you replace it with a normal DateFormatter() you can specify date format. See below code:

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)
like image 179
mlidal Avatar answered Dec 31 '22 05:12

mlidal


This is another answer with an updated POST structure.

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)

If the format is hh:mm dd.MM.yyyy . The updated answer is like this:

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"),
               ]
like image 21
E.Coms Avatar answered Dec 31 '22 05:12

E.Coms