I found online how to sort an array based off another array and it works flawlessly
let english = ["June 12, 2019", "August 12, 2018", "June 1, 2018", "July 18, 2018", "May 4, 2018"]
let ints = [3, 5, 4, 1, 2]
let doubles = [3.0, 5.0, 4.0, 1.0, 2.0]
let roman = ["III", "V", "IV", "I", "II"]
let offsets = english.enumerated().sorted { $0.element < $1.element }.map { $0.offset }
let sorted_english = offsets.map { english[$0] }
let sorted_ints = offsets.map { ints[$0] }
let sorted_doubles = offsets.map { doubles[$0] }
let sorted_roman = offsets.map { roman[$0] }
print(sorted_english)
print(sorted_ints)
print(sorted_doubles)
print(sorted_roman)
It prints the following
["August 12, 2018", "July 18, 2018", "June 1, 2018", "June 12, 2019", "May 4, 2018"]
[5, 1, 4, 3, 2]
[5.0, 1.0, 4.0, 3.0, 2.0]
["V", "I", "IV", "III", "II"]
The english array is sorted by alphabetically. I want to sort that array by date. Here is the code I have that will do that
let formatter : DateFormatter = {
let df = DateFormatter()
df.locale = Locale(identifier: "en_US_POSIX")
df.dateFormat = "MMMM yyyy"
return df
}()
let sortedMonthArray = english.sorted( by: { formatter2.date(from: $0)! < formatter2.date(from: $1)! })
How can I sort the other arrays based on the sortedMonthArray?
Date, notString.Date. The only time they should be a string is to display it to the user.Dateobjects on each comparison.