How to sort a list in Scala by two fields?

Posted by Twistleton on Stack Overflow See other posts from Stack Overflow or by Twistleton
Published on 2012-04-05T11:16:40Z Indexed on 2012/04/05 11:29 UTC
Read the original article Hit count: 302

how to sort a list in Scala by two fields, in this example I will sort by lastName and firstName?

case class Row(var firstName: String, var lastName: String, var city: String)

var rows = List(new Row("Oscar", "Wilde", "London"),
                new Row("Otto",  "Swift", "Berlin"),
                new Row("Carl",  "Swift", "Paris"),
                new Row("Hans",  "Swift", "Dublin"),
                new Row("Hugo",  "Swift", "Sligo"))

rows.sortBy(_.lastName)

I try things like this

rows.sortBy(_.lastName + _.firstName)

but it doesn't work. So I be curious for a good and easy solution.

Thanks in advance!

Pongo

© Stack Overflow or respective owner

Related posts about scala

Related posts about sorting