You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Sreekanth Jella <js...@gmail.com> on 2016/11/18 07:15:01 UTC

sort descending with multiple columns

Hi,

I'm trying to sort multiple columns and column names are dynamic.

df.sort(colList.head, colList.tail: _*)


But I'm not sure how to sort in descending order for all columns, I tried
this but it's for only first column..

df.sort(df.col(colList.head).desc)
How can I pass all column names (or some) with descending order.


Thanks,
Sreekanth

Re: sort descending with multiple columns

Posted by Sreekanth Jella <js...@gmail.com>.
Yes, thank you.

Thanks,
Sreekanth,
+1 (571) 376-0714

On Nov 18, 2016 6:33 AM, "Stuart White" <st...@gmail.com> wrote:

> Is this what you're looking for?
>
> val df = Seq(
>   (1, "A"),
>   (1, "B"),
>   (1, "C"),
>   (2, "D"),
>   (3, "E")
> ).toDF("foo", "bar")
>
> val colList = Seq("foo", "bar")
> df.sort(colList.map(col(_).desc): _*).show
>
> +---+---+
> |foo|bar|
> +---+---+
> |  3|  E|
> |  2|  D|
> |  1|  C|
> |  1|  B|
> |  1|  A|
> +---+---+
>
> On Fri, Nov 18, 2016 at 1:15 AM, Sreekanth Jella <js...@gmail.com>
> wrote:
> > Hi,
> >
> > I'm trying to sort multiple columns and column names are dynamic.
> >
> > df.sort(colList.head, colList.tail: _*)
> >
> >
> > But I'm not sure how to sort in descending order for all columns, I tried
> > this but it's for only first column..
> >
> > df.sort(df.col(colList.head).desc)
> > How can I pass all column names (or some) with descending order.
> >
> >
> > Thanks,
> > Sreekanth
>

Re: sort descending with multiple columns

Posted by Rabin Banerjee <de...@gmail.com>.
++Stuart

val colList = df.columns

can be used

On Fri, Nov 18, 2016 at 8:03 PM, Stuart White <st...@gmail.com>
wrote:

> Is this what you're looking for?
>
> val df = Seq(
>   (1, "A"),
>   (1, "B"),
>   (1, "C"),
>   (2, "D"),
>   (3, "E")
> ).toDF("foo", "bar")
>
> val colList = Seq("foo", "bar")
> df.sort(colList.map(col(_).desc): _*).show
>
> +---+---+
> |foo|bar|
> +---+---+
> |  3|  E|
> |  2|  D|
> |  1|  C|
> |  1|  B|
> |  1|  A|
> +---+---+
>
> On Fri, Nov 18, 2016 at 1:15 AM, Sreekanth Jella <js...@gmail.com>
> wrote:
> > Hi,
> >
> > I'm trying to sort multiple columns and column names are dynamic.
> >
> > df.sort(colList.head, colList.tail: _*)
> >
> >
> > But I'm not sure how to sort in descending order for all columns, I tried
> > this but it's for only first column..
> >
> > df.sort(df.col(colList.head).desc)
> > How can I pass all column names (or some) with descending order.
> >
> >
> > Thanks,
> > Sreekanth
>
> ---------------------------------------------------------------------
> To unsubscribe e-mail: user-unsubscribe@spark.apache.org
>
>

Re: sort descending with multiple columns

Posted by Stuart White <st...@gmail.com>.
Is this what you're looking for?

val df = Seq(
  (1, "A"),
  (1, "B"),
  (1, "C"),
  (2, "D"),
  (3, "E")
).toDF("foo", "bar")

val colList = Seq("foo", "bar")
df.sort(colList.map(col(_).desc): _*).show

+---+---+
|foo|bar|
+---+---+
|  3|  E|
|  2|  D|
|  1|  C|
|  1|  B|
|  1|  A|
+---+---+

On Fri, Nov 18, 2016 at 1:15 AM, Sreekanth Jella <js...@gmail.com> wrote:
> Hi,
>
> I'm trying to sort multiple columns and column names are dynamic.
>
> df.sort(colList.head, colList.tail: _*)
>
>
> But I'm not sure how to sort in descending order for all columns, I tried
> this but it's for only first column..
>
> df.sort(df.col(colList.head).desc)
> How can I pass all column names (or some) with descending order.
>
>
> Thanks,
> Sreekanth

---------------------------------------------------------------------
To unsubscribe e-mail: user-unsubscribe@spark.apache.org