-1

I have an dictionary var dic = [TimeInterval: [CustomObject: Double]]()

TimeInterval is a timestamp. CustomObject is an object with property id inside (of type String). And AudioLevel is a double. I succeed to print object inside with:

for (key, values) in dic {
    for value in values {
        print("\(key): participant id : \(value.key.id) ... AudioLevel: \(value.value)")
    }
}

It prints in my example :

1581361504.064956: participant id : id1 ... AudioLevel: 1.8426420022371925
1581361504.0642018: participant id : id2 ... AudioLevel: 0.0030890756301589217
1581361504.065456: participant id : id2 ... AudioLevel: 4.256374635644427
1581361504.061193: participant id : id1 ... AudioLevel: 1.744451095262131

Now I would like to have an dictionary that contains [CustomObject: Int] where Int is the rank of the participant. Here id2 will be first (4.256374635644427 + 0.0030890756301589217 > 1.744451095262131 + 1.8426420022371925)

Maybe I have to do a reduce based on id + sum of audiolevel, and after to flatMap to retrieve my CustomObject?

Alexander
  • 59,041
  • 12
  • 98
  • 151
Vjardel
  • 1,065
  • 1
  • 13
  • 28
  • What does "rank" mean? – Alexander Feb 10 '20 at 19:29
  • @Alexander-ReinstateMonica Rank is an Int based on AudioLevel sum (if sum of AudioLevel for id1 is greater than sum of AudioLevel for id2, rank will be 1 for id1 and it will be 2 for id2). – Vjardel Feb 10 '20 at 19:41
  • So is `TimeInterval` irrelevant to the output? – Alexander Feb 10 '20 at 19:47
  • @Alexander-ReinstateMonica yes I don’t need TimeInterval – Vjardel Feb 10 '20 at 19:51
  • 1
    So it looks like there's 6 simple parts to this: 1) Flatten out the top level dictionary, to get rid of the grouping by time interval. 2) Group by custom object, 3) sum the `AudioLevels` per custom object, 5) sort them, 6) convert the ordered result into a dict of custom object to rank. Which parts of that can you implement yourself? – Alexander Feb 10 '20 at 19:56
  • @Alexander-ReinstateMonica I succeed the 1) dic.flatMap { $0.value } . After I think I have to group with .map ? I tried map { $0.key.id == $1.key.id } to group by CustomObject.id but that's not correct. – Vjardel Feb 10 '20 at 20:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207547/discussion-between-vjardel-and-alexander-reinstate-monica). – Vjardel Feb 10 '20 at 20:08
  • @Alexander-ReinstateMonica Can you post an answer ? I can't succeed to filter with key.id. After I will mark your answer as correct. – Vjardel Feb 10 '20 at 22:36

1 Answers1

1
dic.forEach { print($0.value) }
koen
  • 5,383
  • 7
  • 50
  • 89