How to sort a list in Scala by two fields?

前端 未结 4 1324
执念已碎
执念已碎 2020-12-02 07:09

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,         


        
4条回答
  •  情话喂你
    2020-12-02 07:49

    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.

提交回复
热议问题