Archer Archer - 1 month ago 22
Scala Question

Sort Array of Int

I am trying to

sort
the following
array
in
descending
order but can't figure out how.

I have tried using
.sort
and
.sortWith
but they don't appear to be applicable to
arrays
?

val result = postIdCount.withFilter(_._2 > 5).map(_._1.toInt)

result.collect

Array[Int] = Array(41, 974, 662, 9554, 116, 4942, 410, 2269, 5443, 5357, 9435, 2293, 266, 711, 441, 61, 3738, 22, 6318, 8390, 497, 19, 9364, 412, 893, 334, 9000, 678, 313, 253, 979, 842, 4914, 2651, 6547, 6576, 1159, 5224, 1107, 52, 810, 361, 694, 739, 904, 5706, 422, 778, 9818, 758, 130, 265, 6107, 155, 2618, 8941, 8963, 834, 326, 731, 2368, 430, 1253)


Would anyone know how I might achieve this?

Thank you for your help.

EDIT: This is what I have so far:

When I try and add:

val result = postIdCount.withFilter(_._2 > 5).map(_._1.toInt).sorted(Ordering[Integer].reverse)


I get an error saying:

error: value sorted is not a member of org.apache.spark.rdd.RDD[Int]

Answer
val sorted = postIdCount
   .withFilter(_._2 > 5)
   .map(_._1.toInt)
   .sortBy(identity, ascending = false)

This returns a sorted RDD[Int].