繁体   English   中英

Swift 按值排序字典作为数组

[英]Swift sort dictionary by value as Array

大家好,我有包含以日期开头的字段的 struct Datas() 我有 24 个对象,我想将它添加到集合并按时间排序集合( .start字段)。 我尝试了堆栈中的答案,但这不是我的情况。

var todaysTimes = [Int:[Datas]]()


struct Datas {

var id: Int
var isVisited: Bool
var start: Date
var end: Date
}

配置单元

private func configureCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {

    let availableSessionTimeCell = collectionView.dequeueReusableCell(withReuseIdentifier: availableSessionCell, for: indexPath) as! EVAvailableSessionTimeCell
    let dataItem = todaysTimes[clinicSection[indexPath.section]!]![indexPath.row]

   availableSessionTimeCell.dateLabel.text = Date.time(day: dataItem.start)

    return cell
}

据我了解,您想字典中的对象数据数组进行排序 但不对dictionary本身进行排序。 如果您想对字典key-value每个value (即[Datas] )进行排序,那么在您的viewDidLoad()您可能可以根据需要对数据中的数组进行排序( ascendingdescending )。

您可以通过在字典中循环并以如下方式对值进行排序来实现它:

for (id, datas) in todaysTimes {
        todaysTimes[id] = datas.sorted(by: { $0.start.compare($1.start) == .orderedDescending })
    }

有关完整示例,您可以在http://online.swiftplayground.run/ 中尝试:

struct Datas {
    var id: Int
    var isVisited: Bool
    var start: Date
    var end: Date
}

// Dump data to show an example
var todaysTimes = [Int:[Datas]]()
let today = Date()
let one_day_before_today = Calendar.current.date(byAdding: .day, value: -1, to: today)!
let two_day_before_today = Calendar.current.date(byAdding: .day, value: -2, to: today)!
todaysTimes[1] = [Datas(id: 1, isVisited: false, start: one_day_before_today, end: Date()), Datas(id: 1, isVisited: false, start: today, end: Date()), Datas(id: 1, isVisited: false, start: two_day_before_today, end: Date())]

// Sort Descending
print("Sorted descending")
for (id, datas) in todaysTimes {
    todaysTimes[id] = datas.sorted(by: { $0.start.compare($1.start) == .orderedDescending })
}
print(todaysTimes)

// Sort Ascending
print("Sorted ascending")
for (id, datas) in todaysTimes {
    todaysTimes[id] = datas.sorted(by: { $0.start.compare($1.start) == .orderedAscending })
}
print(todaysTimes)

// Will print these two lines
// Sorted descending
// [1: [SwiftPlayground.Datas(id: 1, isVisited: false, start: 2019-06-21 12:13:34 +0000, end: 2019-06-21 12:13:34 +0000), SwiftPlayground.Datas(id: 1, isVisited: false, start: 2019-06-20 12:13:34 +0000, end: 2019-06-21 12:13:34 +0000), SwiftPlayground.Datas(id: 1, isVisited: false, start: 2019-06-19 12:13:34 +0000, end: 2019-06-21 12:13:34 +0000)]]
// Sorted ascending
// [1: [SwiftPlayground.Datas(id: 1, isVisited: false, start: 2019-06-19 12:13:34 +0000, end: 2019-06-21 12:13:34 +0000), SwiftPlayground.Datas(id: 1, isVisited: false, start: 2019-06-20 12:13:34 +0000, end: 2019-06-21 12:13:34 +0000), SwiftPlayground.Datas(id: 1, isVisited: false, start: 2019-06-21 12:13:34 +0000, end: 2019-06-21 12:13:34 +0000)]]
// Try the example in online.swiftplayground.run

您必须在调用 configureCell 之前对数组进行排序。 所以在你的 viewDidLoad 方法中你应该使用这样的东西。

dates.sort(by: {(p1: Datas, p2: Datas) -> Bool in
        return p1.start > p2.start
    })

在此之后,您就可以开始了。

不幸的是,对字典进行排序更难实现。

在这个问题中进行了讨论。

你只需要使用forEach(_:)sorted(_:)的组合来forEach(_:)工作,即

var todaysTimes = [Int:[Datas]]()
todaysTimes.forEach { (key,value) in
    let newValue = value.sorted(by: { $0.start < $1.start }) //will sort in ascending order
    todaysTimes[key] = newValue
}

如果您想按降序对其进行排序,您只需使用>而不是< ,即

let newValue = value.sorted(by: { $0.start > $1.start })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM