如何通过两个字段在Scala中对列表进行排序?

问题描述:

如何在Scala中按两个字段对列表进行排序,在这个例子中,我将按姓氏和名字进行排序?

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.

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