排序数组Int

时间:2016-10-09 08:59:34

标签: scala sorting apache-spark

我在sort订单中尝试array以下descending,但无法弄清楚如何。

我尝试使用.sort.sortWith,但它们似乎不适用于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)

有谁知道我怎么做到这一点?

感谢您的帮助。

编辑:这是我到目前为止所做的:

当我尝试添加:

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

我收到错误说:

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

4 个答案:

答案 0 :(得分:1)

postIdCount.withFilter(_._2 > 5).map(_._1.toInt)为您org.apache.spark.rdd.RDD提供Array

尝试

postIdCount.withFilter(_._2 > 5).map(_._1.toInt).collect.sorted(Ordering[Int].reverse)` 

collect函数将数据集的所有元素作为数组返回。但是这会将所有数据收集到spark集群中的一台机器上。

答案 1 :(得分:1)

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

这将返回已排序的RDD[Int]

答案 2 :(得分:0)

val sortedArray = array.sorted

答案 3 :(得分:0)

val sorted = array.sorted(Ordering[Int].reverse)
相关问题