# Need sort help

**URL:** <https://rubytalk.org/t/need-sort-help/48276>\
**Category:** ruby-talk\
**Created:** [12 August 2008 10:18 UTC](https://rubytalk.org/t/need-sort-help/48276 "2008-08-12T10:18:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raveendran\_P](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@Raveendran\_P](https://rubytalk.org/u/Raveendran_P)\
**Post date:** [12 August 2008 10:18 UTC](https://rubytalk.org/t/need-sort-help/48276/1 "2008-08-12T10:18:18Z")

</div>

Hi All,

a=[["raja",1],["Arun",5],["babu",3]]

I want to sort 1,3,5

So i expect output in this order --\> raja, babu, arun

Any methods available for this ?

Regards,  
P.Raveendran  
RF,Chennai

> **[Raveendran](https://raveendran.wordpress.com/)**
>
> Nothing is impossible...

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [12 August 2008 10:21 UTC](https://rubytalk.org/t/need-sort-help/48276/2 "2008-08-12T10:21:20Z")

</div>

a.sort\_by{|i| i[1]}

Array#sort\_by calls the block for each element of the array, and uses the  
return value as a key to use in sorting.

I hope this helps

Stefano

> **···**
>
> On Tuesday 12 August 2008, jazzez ravi wrote:
> 
> > Hi All,
> > 
> > a=[["raja",1],["Arun",5],["babu",3]]
> > 
> > I want to sort 1,3,5
> > 
> > So i expect output in this order --\> raja, babu, arun
> > 
> > Any methods available for this ?
> > 
> > Regards,  
> > P.Raveendran  
> > RF,Chennai  
> > [http://raveendran.wordpress.com](http://raveendran.wordpress.com)

---

<div class="post-metadata">

**Author:** ![Joost\_Diepenmaat](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@Joost\_Diepenmaat](https://rubytalk.org/u/Joost_Diepenmaat)\
**Post date:** [12 August 2008 10:27 UTC](https://rubytalk.org/t/need-sort-help/48276/3 "2008-08-12T10:27:33Z")

</div>

jazzez ravi \<jazzezravi@gmail.com\> writes:

> Hi All,
> 
> a=[["raja",1],["Arun",5],["babu",3]]
> 
> I want to sort 1,3,5
> 
> So i expect output in this order --\> raja, babu, arun
> 
> Any methods available for this ?

[["raja",1],["Arun",5],["babu",3]].sort {|a,b| a[1] \<=\> b[1] };

Cheers,  
J.

> **···**
>
> --  
> Joost Diepenmaat | blog: [http://joost.zeekat.nl/](http://joost.zeekat.nl/) | work: [http://zeekat.nl/](http://zeekat.nl/)

---

<div class="post-metadata">

**Author:** ![Raveendran\_P](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@Raveendran\_P](https://rubytalk.org/u/Raveendran_P)\
**Post date:** [12 August 2008 10:28 UTC](https://rubytalk.org/t/need-sort-help/48276/4 "2008-08-12T10:28:30Z")

</div>

Hi Stefano,

> a.sort\_by{|i| i[1]}
> 
> Array#sort\_by calls the block for each element of the array, and uses  
> the  
> return value as a key to use in sorting.
> 
> I hope this helps
> 
> Stefano

Thanks for quick and best reply..

Regards,  
P.Raveendran  
RF,Chennai

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
