Scala: Issue w/ sorting -
sort of new scala.. have array:
1,2,3,4,5,6,7,8,9,10
and numbers 6
how achieve in scala?
you can example this:
val l = list(1,2,3,4,5,6,7,8,9,10) l.sortby(num => math.abs(num - 6))
take @ documentation of sortby method of list: http://www.scala-lang.org/api/2.10.3/index.html#scala.collection.immutable.list
sortby
takes argument function defines order. in our case ordering function takes single argument num
, maps distance number 6. distance computed absolute value of 6 substracted given number.
Comments
Post a Comment