0

How do I create one implicit class for both use cases?

  implicit class SortableByIntValue(rdd:RDD[(String,Int)]){
    def sortByValue = rdd.sortBy(_._2)
  }
  implicit class SortableByDoubleValue(rdd:RDD[(String,Double)]){
    def sortByValue = rdd.sortBy(_._2)
  }
zero323
  • 322,348
  • 103
  • 959
  • 935
Walrus the Cat
  • 2,314
  • 5
  • 35
  • 64

1 Answers1

1

Try this:

import scala.reflect.ClassTag

implicit class SortableByValue[N : Numeric : ClassTag](rdd:RDD[(String,N)]){
  def sortByValue = rdd.sortBy(_._2)
}

This should be extensible to any type for which an Ordering is available ([O : Ordering] instead of [N : Numeric]), if desired.

zero323
  • 322,348
  • 103
  • 959
  • 935
Shadowlands
  • 14,994
  • 4
  • 45
  • 43