快速按值将字典排序为数组

时间:2019-06-21 11:47:58

标签: swift sorting uicollectionview

大家好,我有结构 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
}

3 个答案:

答案 0 :(得分:2)

据我了解,您想在字典中对对象数据数组进行排序。但不对dictionary本身进行排序。如果您想对字典value中的每个[Datas](即key-value)进行排序,则可以在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

答案 1 :(得分:1)

您必须在调用configureCell之前对数组进行排序。因此,在您的viewDidLoad方法中,您应该使用类似这样的内容。

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

在这之后,你很好。

不幸的是,很难对字典进行排序。

question中对此进行了讨论。

答案 2 :(得分:1)

您只需使用forEach(_:)sorted(_:)的组合来使该功能正常工作,即

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