Taras Tomchuk
Taras Tomchuk

Reputation: 331

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?

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:

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.

Upvotes: 1

Views: 99

Answers (1)

Manav
Manav

Reputation: 2522

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.

Upvotes: 1

Related Questions