scala sort a set of a certain class -
I have a set of fixed DTOs.
The name of the DTO is "numeric value", and the following class members are: 1) time: long 2) value: double
I have a set named "points", and I want to sort it according to increasing time, i.e. 0, 1, 2, 4, 100, ....
The attachment code is:
val point: Set (Numeric Value) = NumericWallulate Val SetSorted = Archive. Immmoot Soratset [Long] (++) Issue
I did the following:
Although the result is not ordered according to time.
Thanks
The result is not reserved because the order for your class
is not.
You can explicitly be ordered
:
case class NumericValue (time: long, value: double) command [NumericValue ] {Compare Override DF (Joe: NumericValue): Int = Command [Long] .Compare (this.time, that.time)}
orOrder
create:val set = sorted set [NumericValue] () (New command [NumericValue] () {override def compare (X: Anumeric Value, Y: Anmericvalue): int = Order [long] .Compare (X.time, Y. time)})
The result should be ordered:
set + numeric value (345, 45) + numeric value (453, 123) + numeric value (5, 5) res0: scala. Collection.SortedSet [NumericValue] = TreeSet (NumericValue (5,5.0), Numeric Value (345), 45.0), Numeric Value (453123.0))
Comments
Post a Comment