Forum: Ruby Sorting an array of arrays

80159acdf89580a20e8e835772b5bf82?d=identicon&s=25 Doug Jolley (ddjolley)
on 2013-09-22 20:51
I have an array of arrays.  The first field of each contained array is a
date.  The second field of each contained array is the corresponding day
of the week.  I thought that it would be very easy to sort the outer
array on the dates like this:

myArray=[["2013/09/09","Mon"],["2013/09/14","Fri"],["2013/09/11","Wed"]]
myArray.sort {|x,y| x[0] <=> y[0]}
puts(myArray.inspect)

It's not working for me.  Can someone please tell me what I'm missing?
Thanks for any help.

     ... doug
99804651e19d8b0dffbc0da53e39114e?d=identicon&s=25 Adam Prescott (Guest)
on 2013-09-22 20:56
(Received via mailing list)
ary.sort won't modify ary in-place.

Try this instead:

  myArray=[["2013/09/09","Mon"],["2013/09/14","Fri"],["2013/09/11","Wed"]]
  sorted = myArray.sort { |x,y| x[0] <=> y[0] }
  p sorted

You might also want to check out sort! vs sort.
80159acdf89580a20e8e835772b5bf82?d=identicon&s=25 Doug Jolley (ddjolley)
on 2013-09-22 21:42
sort! is definitely the way to go.  Thanks so much for the help.  (I
should have been able to figure that out.) :-)

      ... doug
173954aae708a4997602bb8c4d14b3de?d=identicon&s=25 Carlos Guillen (Guest)
on 2013-09-22 22:01
(Received via mailing list)
I think you should try :

myArray=[["2013/09/09","Mon"],["2013/09/14","Fri"],["2013/09/11","Wed"]]
myArray.sort! {|x,y| x[0] <=> y[0]}
puts(myArray.inspect)

notice, sort!

Carlos G

On Sep 22, 2013, at 01:51 PM, Doug Jolley <lists@ruby-forum.com> wrote:

I have an array of arrays. The first field of each contained array is a
date. The second field of each contained array is the corresponding day
of the week. I thought that it would be very easy to sort the outer
array on the dates like this:

myArray=[["2013/09/09","Mon"],["2013/09/14","Fri"],["2013/09/11","Wed"]]
myArray.sort {|x,y| x[0] <=> y[0]}
puts(myArray.inspect)

It's not working for me. Can someone please tell me what I'm missing?
Thanks for any help.

... doug
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-23 00:23
There's always sort_by, as well.
myArray.sort_by! { |x| x[0] }
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-09-23 09:06
(Received via mailing list)
On Sun, Sep 22, 2013 at 8:51 PM, Doug Jolley <lists@ruby-forum.com>
wrote:
> I have an array of arrays.  The first field of each contained array is a
> date.

No.  It's a String.

>  The second field of each contained array is the corresponding day
> of the week.  I thought that it would be very easy to sort the outer
> array on the dates like this:
>
> myArray=[["2013/09/09","Mon"],["2013/09/14","Fri"],["2013/09/11","Wed"]]
> myArray.sort {|x,y| x[0] <=> y[0]}
> puts(myArray.inspect)
>
> It's not working for me.  Can someone please tell me what I'm missing?
> Thanks for any help.

Apart from using sort! you can use sort_by!

irb(main):012:0> a =
[["2013/09/09","Mon"],["2013/09/14","Fri"],["2013/09/11","Wed"]]
=> [["2013/09/09", "Mon"], ["2013/09/14", "Fri"], ["2013/09/11", "Wed"]]
irb(main):013:0> a.sort_by! {|x| Date.parse(x[0])}
=> [["2013/09/09", "Mon"], ["2013/09/11", "Wed"], ["2013/09/14", "Fri"]]
irb(main):014:0> a
=> [["2013/09/09", "Mon"], ["2013/09/11", "Wed"], ["2013/09/14", "Fri"]]

But I'd rather make Date elements part of the Array.  Then you have
the proper representation of the data.  String is just for input and
output.

Kind regards

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.