Sort a Dataset[String] in Scala

I have dsString: Dataset[(String,Long)] (No DataFrame or Dataset[Row]) and I'm trying to order by Long .orderBy(_._2)

My problem is that .orderBy() and .sort() only accept columns, and I can only use .sortBy with RDDs.

DataFrame solution

dsString.toDF("a", "b")
  .groupBy("b")

RDD solution:

 dsString.toJavaRDD
    .sortBy(_._2)

How could Dataset[(String,Long)] do the same?

Upvotes: 0

Views: 377

Answers (1)

Lamanus
Lamanus

Reputation: 13551

Dataset also can be applied orderBy. For example,

+---+---+
| _1| _2|
+---+---+
|  c|  3|
|  b|  5|
|  a|  4|
+---+---+

this is my Dataset and

df2.orderBy(col("_1").desc).show
df2.orderBy(col("_2").asc).show

give the results as follows:

+---+---+
| _1| _2|
+---+---+
|  c|  3|
|  b|  5|
|  a|  4|
+---+---+


+---+---+
| _1| _2|
+---+---+
|  c|  3|
|  a|  4|
|  b|  5|
+---+---+

Upvotes: 1

Related Questions