根据另一个数组对数组进行排序(按日期排序)

时间:2018-07-23 23:33:43

标签: arrays swift sorting

我在网上发现了如何根据另一个数组对数组进行排序,并且它可以完美地工作

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)

它将打印以下内容

["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"]

english数组按字母顺序排序。我想按日期对该数组进行排序。这是我要执行的代码

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)! })

如何根据sortedMonthArray对其他数组进行排序?

1 个答案:

答案 0 :(得分:1)

您需要提供正确的date format才能获取格式化的日期。其余的操作如下:

    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 formatter : DateFormatter = {
        let df = DateFormatter()
        df.locale = Locale(identifier: "en_US_POSIX")
        df.dateFormat = "MMMM dd, yyyy"
        return df
    }()

    let dates = english.map({ formatter.date(from: $0) })
    let offsets = dates.enumerated().sorted( by: { $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)

输出:

["May 4, 2018", "June 1, 2018", "July 18, 2018", "August 12, 2018", "June 12, 2019"]
[2, 4, 1, 5, 3]
[2.0, 4.0, 1.0, 5.0, 3.0]
["II", "IV", "I", "V", "III"]

建议:

以上方法非常繁琐,无法建议您进行更好的设计。

创建一个struct并具有以下所有相关成员,

struct MyStruct {

    // MARK: - Private properties
    private var dateFormatter: DateFormatter = {
        let df = DateFormatter()
        df.locale = Locale(identifier: "en_US_POSIX")
        df.dateFormat = "MMMM dd, yyyy"
        return df
    }()

    // MARK: - Public properties
    public var date: Date!
    public var index: Int = 0
    public var value: Double = 0.0
    public var roman: String
    public var dateString: String

    // MARK: - Initialization
    init(_ dateString: String, index: Int, value: Double, roman: String) {
        self.dateString = dateString
        self.index = index
        self.value = value
        self.roman = roman
        self.date = self.dateFormatter.date(from: self.dateString)
    }
}

现在您可以如下创建一个array中的单个MyStruct并按日期排序,

    let data = [
        MyStruct("June 12, 2019",   index: 3, value: 3.0, roman: "III"),
        MyStruct("August 12, 2018", index: 5, value: 5.0, roman: "V"),
        MyStruct("June 1, 2018",    index: 4, value: 4.0, roman: "IV"),
        MyStruct("July 18, 2018",   index: 1, value: 1.0, roman: "I"),
        MyStruct("May 4, 2018",     index: 2, value: 2.0, roman: "II")
    ]
    let sorted = data.sorted(by: { $0.date < $1.date })
    sorted.forEach({ print("\($0.index): \($0.roman): \($0.value): \($0.dateString)")})

结果:

2: II: 2.0: May 4, 2018
4: IV: 4.0: June 1, 2018
1: I: 1.0: July 18, 2018
5: V: 5.0: August 12, 2018
3: III: 3.0: June 12, 2019