Home > Blockchain >  How do I add a ranking number to my tuples?
How do I add a ranking number to my tuples?

Time:10-15

My data:

(John, 23)
(Micheal, 8)
(Kathy, 8)
(Sam, 18)

I need to sort them by value followed by key. Then, I need to assign an order number for them:

(0, John, 23)
(1, Sam, 18)
(2, Kathy, 8)
(3, Micheal, 8)

This is my current code, data contains the tuples:

data.sortBy(x => (-x._2, x._1))

I'm not sure how to add the rankings to the front

CodePudding user response:

You can do this with zipWithIndex.

  • Related