In SparkSQL, you do not have a choice and need to use orderBy
with one or more column(s). With RDDs, you can use a custom java-like comparator if you feel like it. Indeed, here is the signature of the sortBy
method of an RDD
(cf the scaladoc of Spark 2.4):
def sortBy[K](f: (T) ⇒ K, ascending: Boolean = true, numPartitions: Int = this.partitions.length)
(implicit ord: Ordering[K], ctag: ClassTag[K]): RDD[T]
This means that you can provide an Ordering
of your choice, which is exactly like a java Comparator
(Ordering
actually inherit from Comparator
).
For simplicity, let's say I want to sort by absolute value of a column 'x' (this can be done without a comparator, but let's assume I need to use a comparator). I start by defining my comparator on rows:
class RowOrdering extends Ordering[Row] {
def compare(x : Row, y : Row): Int = x.getAs[Int]("x").abs - y.getAs[Int]("x").abs
}
Now let's define data and sort it:
val df = Seq( (0, 1),(1, 2),(2, 4),(3, 7),(4, 1),(5, -1),(6, -2),
(7, 5),(8, 5), (9, 0), (10, -9)).toDF("id", "x")
val rdd = df.rdd.sortBy(identity)(new RowOrdering(), scala.reflect.classTag[Row])
val sorted_df = spark.createDataFrame(rdd, df.schema)
sorted_df.show
+---+---+
| id| x|
+---+---+
| 9| 0|
| 0| 1|
| 4| 1|
| 5| -1|
| 6| -2|
| 1| 2|
| 2| 4|
| 7| 5|
| 8| 5|
| 3| 7|
| 10| -9|
+---+---+
Another solution is to define an implicit ordering so that you don't need to provide it when sorting.
implicit val ord = new RowOrdering()
df.rdd.sortBy(identity)
Finally, note that df.rdd.sortBy(_.getAs[Int]("x").abs)
would achive the same result. Also, you can use tuple ordering to do more complex things such as order by absolute values, and if equal, put the positive values first:
df.rdd.sortBy(x => (x.getAs[Int]("x").abs, - x.getAs[Int]("x"))) //RDD
df.orderBy(abs($"x"), - $"x") //dataframe