Mehaboob Khan
Mehaboob Khan

Reputation: 353

How to sort spark dataframe on the combination of columns in Java?

I have a spark data frame in Java, something like below:

enter image description here

I want it to be sorted based on "Col3" but all the values of Col1 and Col2 should be in a group. The result should be something like below:

enter image description here

Upvotes: 0

Views: 437

Answers (1)

Shrey Jakhmola
Shrey Jakhmola

Reputation: 532

The groupBy() function is used during aggregation while your requirement just requires orderBy()

Assuming dataframe df with 3 columns Col1, Col2, Col3, you can do the below in Spark

val sortedDf = df.orderBy(col("Col1").desc,col("Col2").desc,col("Col3").asc)

POC for the same is available here SQLFIDDLE

Upvotes: 1

Related Questions