Simplest way to sort list of objects

后端 未结 3 814
情话喂你
情话喂你 2021-02-12 17:40

I have a list of objects of type A. In a first iteration I assign each object a double value 0 < x < 1 and then want to sort each object according to it\'s x value.

<
3条回答
  •  别跟我提以往
    2021-02-12 18:26

    You can actually do this quite easily with normal Scala lists and their sortBy method. Here's a brief REPL session showing how:

    scala> class A(val value: Double) { override def toString = "A:" + value }
    defined class A
    
    scala> List(new A(6), new A(1), new A(3)) sortBy (_.value)
    res0: List[A] = List(A:1.0, A:3.0, A:6.0)
    

提交回复
热议问题