I want to extend a sort method for scala Array which can return the original indecies.
The code I writed below:
object ArrayExtend {
implicit class ArrayExtension(val array: Array[Int]) {
def argSort(): Array[Int] = {
var tupleArray = for (i <- 0 until array.length) yield (i, array(i))
tupleArray = tupleArray.sortWith((x,y)=> x._2 < y._2)
val result = for((index,_) <- tupleArray) yield index
return result.toArray
}
}
def main(args:Array[String]){
val array = Array(5,4,3,2,0)
for(i <- array.argSort()) print(i)
}
}
This code works on Array[Int], how can I extend this method to all different types of Array? I didn't find any compare method in the AnyRef class.