Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Need sort help

Reply
Thread Tools

Need sort help

 
 
jazzez ravi
Guest
Posts: n/a
 
      08-12-2008
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      08-12-2008
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


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


 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      08-12-2008
jazzez ravi <> 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/ | work: http://zeekat.nl/
 
Reply With Quote
 
jazzez ravi
Guest
Posts: n/a
 
      08-12-2008
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
http://raveendran.wordpress.com
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP!! anyone ??can help me about my project "quick sort implemented with shell sort? comsciepartner General Computer Support 0 10-06-2008 01:02 PM
Re: When will Thunderbird support sort in place (in context sort)? Ron Natalie Firefox 0 02-02-2006 04:38 AM
Array sort function sorts on chars not numbers ... help ! how to sort numbers GIMME Javascript 5 07-26-2004 01:28 AM
xsl:sort lang="es" modern vs. tradidional Spanish sort order nobody XML 0 06-01-2004 06:25 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57