[英]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()
您可能可以根據需要對數據中的數組進行排序( ascending
或descending
)。
您可以通過在字典中循環並以如下方式對值進行排序來實現它:
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.