简体   繁体   English

如何对存储在字典中的元组内的数组进行排序

[英]How to sort array inside tuple stored in dictionary

I have such dictionary of tuples:我有这样的元组字典:

var items = [(String, [LogSymptomTemp])]()

I'm using it to display grouped table view with sections.我正在使用它来显示带有部分的分组表视图。

How can I sort items of LogSymptomTemp array by severity?如何按严重性对LogSymptomTemp数组的项目进行排序?

struct LogSymptomTemp {

var symptomId = String()
var symptomName = String()
var trackedInRow = Int()
var severity = Int() }

As temp solution, I'm sorting inside my tableView cellForRowAt method, but that's not a place to do that:作为临时解决方案,我在tableView cellForRowAt方法中进行排序,但这不是这样做的地方:

let logItems = items[indexPath.section].1

let finalList = logItems.sorted(by: { $0.severity > $1.severity })

I'm used to simple data structs and I can do simple sorting, but I'm struggling with this one, as it's not so obvious for me.我习惯了简单的数据结构,我可以进行简单的排序,但我正在努力解决这个问题,因为它对我来说并不那么明显。

You can use:您可以使用:

for var item in items {
   item.1.sort(by: { $0.severity > $1.severity })
 }

making each item as variable helps in mutating each item as sort mutates the array present in tuple.将每个项目作为变量有助于改变每个项目,因为 sort 会改变元组中存在的数组。

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

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