krish
krish

Reputation: 41

Sorting Scala enumerator by using name

I have a writing a Scala enumerator and trying to sort it out but it is sorting by id not by name.

object PaymentMethodEnum extends Enumeration {
  val text1 = Value(2,"value1")
  val text2 = Value(0,"value2")
  val text3 = Value(1,"value3")
  val text4 = Value(4,"value4")
  val text5 = Value(3,"value5")
}

when I try to PaymentMethodEnum.values.toSeq the values are displayed in the order value2,value3,value1,value5,value4.

I am trying to display the values in order like value1,value2,value3,value4,value5. I have tried sorting with many option but no luck.

Upvotes: 1

Views: 107

Answers (1)

jwvh
jwvh

Reputation: 51271

If you simply want to display a ValueSet ordered by "name" instead of by id:

PaymentMethodEnum.values.map(_.toString)
//res0: SortedSet[String] = TreeSet(value1, value2, value3, value4, value5)

Upvotes: 1

Related Questions