Search code examples
listscalalistbuffer

Sort tuple ListBuffer


So i have this ListBuffer with 2 values: String and BigDecimal, after populate my List i want to sort it before print:

var data = new ListBuffer[(String, BigDecimal)]
data+=(("se", 34))
data+=(("sh", 4))
data+=(("fjd", 33))
data+=(("dhdh", 24))
data+=(("dhd", 125))

And i wonder how to sort this according BigDecimal value so this is what i have try:

val list = data.map(x=>x._2 > x._2)

Solution

  • You can use sortBy and use the ordering of the value of the second element as following,

    data = data.sortBy(_._2)