sorting an array w multiple values

J

Josselin

I understand how to sort an array with a single value but how can I
sort an array w multiple values : [ integer_value1, float_value2]

an_array = [ [123, 42.50], [ 245, 25.45], [389, 32.69] ]

wich should give (sorting on the 2nd value)

sorted_array => [[ 245, 25.45],  [389, 32.69], [123, 42.50] ]

thanks for your help

joss
 
D

dblack

---2049402039-1722804141-1166459366=:5255
Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-1722804141-1166459366=:5255"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

---2049402039-1722804141-1166459366=:5255
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

I understand how to sort an array with a single value but how can I sort = an=20
array w multiple values : [ integer_value1, float_value2]

an_array =3D [=A0[123, 42.50], [ 245, 25.45], [389, 32.69] ]

wich should give (sorting on the 2nd value)

sorted_array =3D> [[ 245, 25.45], =A0[389, 32.69], [123, 42.50] ]

sorted_array =3D an_array.sort_by {|e| e[1] }


David

--=20
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
---2049402039-1722804141-1166459366=:5255--
---2049402039-1722804141-1166459366=:5255--
 
J

Josselin

---2049402039-1722804141-1166459366=:5255
Content-Type: MULTIPART/MIXED;
BOUNDARY="-2049402039-1722804141-1166459366=:5255"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

---2049402039-1722804141-1166459366=:5255
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

I understand how to sort an array with a single value but how can I sort = an=20
array w multiple values : [ integer_value1, float_value2]

an_array =3D [=A0[123, 42.50], [ 245, 25.45], [389, 32.69] ]

wich should give (sorting on the 2nd value)

sorted_array =3D> [[ 245, 25.45], =A0[389, 32.69], [123, 42.50] ]

sorted_array =3D an_array.sort_by {|e| e[1] }


David

--=20
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
---2049402039-1722804141-1166459366=:5255--
---2049402039-1722804141-1166459366=:5255--

thanks a lot , got it... written in my secret notebook.... ;-)))
 
J

Josselin

From: (e-mail address removed) [mailto:[email protected]] On
I understand how to sort an array with a single value but how can I sort an
array w multiple values : [ integer_value1, float_value2]

sorted_array = an_array.sort_by {|e| e[1] }

And if you want to sort by multiple values, in your own chosen order:

sorted_array = an_array.sort_by{ |e| [ e[1], e[0] ] }

thanks got it too... I think I am strating to understand the logic
behind Ruby structure...
so now I'll know where to look for before asking .. ;-))

.... but asking and getting answers help others too....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top