[英]Swift sort dictionary by value as Array
Hello guys i have struct Datas() which contains field start with date.大家好,我有包含以日期开头的字段的 struct Datas() 。 I have like 24 objects and i want to add it to collection and sort collection by time ( .start field).
我有 24 个对象,我想将它添加到集合并按时间排序集合( .start字段)。 I tried answers from stack, but it not my case.
我尝试了堆栈中的答案,但这不是我的情况。
var todaysTimes = [Int:[Datas]]()
struct Datas {
var id: Int
var isVisited: Bool
var start: Date
var end: Date
}
configure cell配置单元
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
}
As I understood you would like to sort the array of object Datas in your dictionary.据我了解,您想对字典中的对象数据数组进行排序。 But not sort the
dictionary
itself.但不对
dictionary
本身进行排序。 In case you would like to sort every value
(which is [Datas]
) in your dictionary key-value
then in your viewDidLoad()
probably you could sort the array in your data to be as you wished (either ascending
or descending
).如果您想对字典
key-value
每个value
(即[Datas]
)进行排序,那么在您的viewDidLoad()
您可能可以根据需要对数据中的数组进行排序( ascending
或descending
)。
You can achieve it by looping in your dictionary and sort the values in a way like this:您可以通过在字典中循环并以如下方式对值进行排序来实现它:
for (id, datas) in todaysTimes {
todaysTimes[id] = datas.sorted(by: { $0.start.compare($1.start) == .orderedDescending })
}
For a complete example , you can try this in http://online.swiftplayground.run/ :有关完整示例,您可以在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
You have to sort your array before calling configureCell.您必须在调用 configureCell 之前对数组进行排序。 So in your viewDidLoad method you should use something like this.
所以在你的 viewDidLoad 方法中你应该使用这样的东西。
dates.sort(by: {(p1: Datas, p2: Datas) -> Bool in
return p1.start > p2.start
})
After this you are good to go.在此之后,您就可以开始了。
Unfortunately sorting a dictionary is harder to achieve.不幸的是,对字典进行排序更难实现。
You simply need to use a combination of forEach(_:)
and sorted(_:)
to get that working, ie你只需要使用
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
}
In case you want to sort it in descending order , you just using >
instead of <
, ie如果您想按降序对其进行排序,您只需使用
>
而不是<
,即
let newValue = value.sorted(by: { $0.start > $1.start })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.