How to sort a list in Scala by two fields?

ScalaSortingFunctional Programming

Scala Problem Overview


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.

Scala Solutions


Solution 1 - Scala

rows.sortBy(r => (r.lastName, r.firstName))

Solution 2 - Scala

rows.sortBy (row => row.lastName + row.firstName)

If you want to sort by the merged names, as in your question, or

rows.sortBy (row => (row.lastName, row.firstName))

if you first want to sort by lastName, then firstName; relevant for longer names (Wild, Wilder, Wilderman).

If you write

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

with 2 underlines, the method expects two parameters:

<console>:14: error: wrong number of parameters; expected = 1
       rows.sortBy (_.lastName + _.firstName)
                               ^

Solution 3 - Scala

In general, if you use a stable sorting algorithm, you can just sort by one key, then the next.

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

The final result will be sorted by lastname, then where that is equal, by firstname.

Solution 4 - Scala

Perhaps this works only for a List of Tuples, but

scala> var zz = List((1, 0.1), (2, 0.5), (3, 0.6), (4, 0.3), (5, 0.1))
zz: List[(Int, Double)] = List((1,0.1), (2,0.5), (3,0.6), (4,0.3), (5,0.1))

scala> zz.sortBy( x => (-x._2, x._1))
res54: List[(Int, Double)] = List((3,0.6), (2,0.5), (4,0.3), (1,0.1), (5,0.1))

appears to work and be a simple way to express it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTwistletonView Question on Stackoverflow
Solution 1 - ScalaseniaView Answer on Stackoverflow
Solution 2 - Scalauser unknownView Answer on Stackoverflow
Solution 3 - ScalaMarcinView Answer on Stackoverflow
Solution 4 - ScalaspreinhardtView Answer on Stackoverflow