How to use orderby() with descending order in Spark window functions?
There are two versions of
orderBy, one that works with strings and one that works with
Column objects (API). Your code is using the first version, which does not allow for changing the sort order. You need to switch to the column version and then call the
desc method, e.g.,
Now, we get into API design territory. The advantage of passing
Column parameters is that you have a lot more flexibility, e.g., you can use expressions, etc. If you want to maintain an API that takes in a string as opposed to a
Column, you need to convert the string to a column. There are a number of ways to do this and the easiest is to use
Putting it all together, we get
Say for example, if we need to order by a column called
Date in descending order in the Window function, use the
$ symbol before the column name which will enable us to use the
After specifying the column name in double quotes, give
.desc which will sort in descending order.