You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Ashok Kumar <as...@yahoo.com.INVALID> on 2016/02/27 19:25:45 UTC

Ordering two dimensional arrays of (String, Int) in the order of second element

 Hello,
I like to be able to solve this using arrays.
I have two dimensional array of (String,Int) with 5  entries say arr("A",20), arr("B",13), arr("C", 18), arr("D",10), arr("E",19)
I like to write a small code to order these in the order of highest Int column so I will have arr("A",20), arr("E",19), arr("C",18) ....
What is the best way of doing this using arrays only?
Thanks

Re: Ordering two dimensional arrays of (String, Int) in the order of second element

Posted by Ashok Kumar <as...@yahoo.com.INVALID>.
no particular reason. just wanted to know if there was another way as well.
thanks 

    On Saturday, 27 February 2016, 22:12, Yin Yang <yy...@gmail.com> wrote:
 

 Is there particular reason you cannot use temporary table ?
Thanks
On Sat, Feb 27, 2016 at 10:59 AM, Ashok Kumar <as...@yahoo.com> wrote:

Thank you sir.
Can one do this sorting without using temporary table if possible?
Best 

    On Saturday, 27 February 2016, 18:50, Yin Yang <yy...@gmail.com> wrote:
 

 scala>  Seq((1, "b", "test"), (2, "a", "foo")).toDF("id", "a", "b").registerTempTable("test")
scala> val df = sql("SELECT struct(id, b, a) from test order by b")df: org.apache.spark.sql.DataFrame = [struct(id, b, a): struct<id: int, b: string ... 1 more field>]
scala> df.show+----------------+|struct(id, b, a)|+----------------+|       [2,foo,a]||      [1,test,b]|+----------------+
On Sat, Feb 27, 2016 at 10:25 AM, Ashok Kumar <as...@yahoo.com.invalid> wrote:

 Hello,
I like to be able to solve this using arrays.
I have two dimensional array of (String,Int) with 5  entries say arr("A",20), arr("B",13), arr("C", 18), arr("D",10), arr("E",19)
I like to write a small code to order these in the order of highest Int column so I will have arr("A",20), arr("E",19), arr("C",18) ....
What is the best way of doing this using arrays only?
Thanks



   



  

Re: Ordering two dimensional arrays of (String, Int) in the order of second element

Posted by Yin Yang <yy...@gmail.com>.
Is there particular reason you cannot use temporary table ?

Thanks

On Sat, Feb 27, 2016 at 10:59 AM, Ashok Kumar <as...@yahoo.com> wrote:

> Thank you sir.
>
> Can one do this sorting without using temporary table if possible?
>
> Best
>
>
> On Saturday, 27 February 2016, 18:50, Yin Yang <yy...@gmail.com> wrote:
>
>
> scala>  Seq((1, "b", "test"), (2, "a", "foo")).toDF("id", "a",
> "b").registerTempTable("test")
>
> scala> val df = sql("SELECT struct(id, b, a) from test order by b")
> df: org.apache.spark.sql.DataFrame = [struct(id, b, a): struct<id: int, b:
> string ... 1 more field>]
>
> scala> df.show
> +----------------+
> |struct(id, b, a)|
> +----------------+
> |       [2,foo,a]|
> |      [1,test,b]|
> +----------------+
>
> On Sat, Feb 27, 2016 at 10:25 AM, Ashok Kumar <
> ashok34668@yahoo.com.invalid> wrote:
>
> Hello,
>
> I like to be able to solve this using arrays.
>
> I have two dimensional array of (String,Int) with 5  entries say
> arr("A",20), arr("B",13), arr("C", 18), arr("D",10), arr("E",19)
>
> I like to write a small code to order these in the order of highest Int
> column so I will have arr("A",20), arr("E",19), arr("C",18) ....
>
> What is the best way of doing this using arrays only?
>
> Thanks
>
>
>
>
>

Re: Ordering two dimensional arrays of (String, Int) in the order of second element

Posted by Ashok Kumar <as...@yahoo.com.INVALID>.
Thank you sir.
Can one do this sorting without using temporary table if possible?
Best 

    On Saturday, 27 February 2016, 18:50, Yin Yang <yy...@gmail.com> wrote:
 

 scala>  Seq((1, "b", "test"), (2, "a", "foo")).toDF("id", "a", "b").registerTempTable("test")
scala> val df = sql("SELECT struct(id, b, a) from test order by b")df: org.apache.spark.sql.DataFrame = [struct(id, b, a): struct<id: int, b: string ... 1 more field>]
scala> df.show+----------------+|struct(id, b, a)|+----------------+|       [2,foo,a]||      [1,test,b]|+----------------+
On Sat, Feb 27, 2016 at 10:25 AM, Ashok Kumar <as...@yahoo.com.invalid> wrote:

 Hello,
I like to be able to solve this using arrays.
I have two dimensional array of (String,Int) with 5  entries say arr("A",20), arr("B",13), arr("C", 18), arr("D",10), arr("E",19)
I like to write a small code to order these in the order of highest Int column so I will have arr("A",20), arr("E",19), arr("C",18) ....
What is the best way of doing this using arrays only?
Thanks



  

Re: Ordering two dimensional arrays of (String, Int) in the order of second element

Posted by Yin Yang <yy...@gmail.com>.
scala>  Seq((1, "b", "test"), (2, "a", "foo")).toDF("id", "a",
"b").registerTempTable("test")

scala> val df = sql("SELECT struct(id, b, a) from test order by b")
df: org.apache.spark.sql.DataFrame = [struct(id, b, a): struct<id: int, b:
string ... 1 more field>]

scala> df.show
+----------------+
|struct(id, b, a)|
+----------------+
|       [2,foo,a]|
|      [1,test,b]|
+----------------+

On Sat, Feb 27, 2016 at 10:25 AM, Ashok Kumar <as...@yahoo.com.invalid>
wrote:

> Hello,
>
> I like to be able to solve this using arrays.
>
> I have two dimensional array of (String,Int) with 5  entries say
> arr("A",20), arr("B",13), arr("C", 18), arr("D",10), arr("E",19)
>
> I like to write a small code to order these in the order of highest Int
> column so I will have arr("A",20), arr("E",19), arr("C",18) ....
>
> What is the best way of doing this using arrays only?
>
> Thanks
>

Re: Ordering two dimensional arrays of (String, Int) in the order of second element

Posted by Yin Yang <yy...@gmail.com>.
Is this what you look for ?

scala> Seq((2, "a", "test"), (2, "b", "foo")).toDF("id", "a",
"b").registerTempTable("test")

scala> val df = sql("SELECT struct(id, b, a) from test")
df: org.apache.spark.sql.DataFrame = [struct(id, b, a): struct<id: int, b:
string ... 1 more field>]

scala> df.show
+----------------+
|struct(id, b, a)|
+----------------+
|      [2,test,a]|
|       [2,foo,b]|
+----------------+

You can adjust the order of the columns in struct() .

FYI

On Sat, Feb 27, 2016 at 10:25 AM, Ashok Kumar <as...@yahoo.com.invalid>
wrote:

> Hello,
>
> I like to be able to solve this using arrays.
>
> I have two dimensional array of (String,Int) with 5  entries say
> arr("A",20), arr("B",13), arr("C", 18), arr("D",10), arr("E",19)
>
> I like to write a small code to order these in the order of highest Int
> column so I will have arr("A",20), arr("E",19), arr("C",18) ....
>
> What is the best way of doing this using arrays only?
>
> Thanks
>