[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Sorting through an array of an array

Dominic Son

10/13/2006 10:15:00 PM

Hello.

Each of my arrays look like this:
container[0] = ["foo", "bar", "baz"]
container[1] = ["doo", "poo", "woo"]

i'd like to sort by the second position (bar, and poo)
and of course,affecting the whole row, so that "bar" should be on top,
along with "foo" and "baz" as container[0].

What method should i use?

Thanks


--
Posted via http://www.ruby-....

5 Answers

emarkp

10/13/2006 10:33:00 PM

0

In article <1a701a2f74c6f4245c0092667a764972@ruby-forum.com>,
Dominic Son <dominicson@gmail.com> wrote:
>Hello.
>
>Each of my arrays look like this:
>container[0] = ["foo", "bar", "baz"]
>container[1] = ["doo", "poo", "woo"]
>
>i'd like to sort by the second position (bar, and poo)
>and of course,affecting the whole row, so that "bar" should be on top,
>along with "foo" and "baz" as container[0].
>
>What method should i use?

container.sort { |x,y| x[1]<=>y[1] }
--
Mark Ping
emarkp@soda.CSUA.Berkeley.EDU

Brian Mitchell

10/13/2006 10:43:00 PM

0

On 10/13/06, Dominic Son <dominicson@gmail.com> wrote:
> Hello.
>
> Each of my arrays look like this:
> container[0] = ["foo", "bar", "baz"]
> container[1] = ["doo", "poo", "woo"]
>
> i'd like to sort by the second position (bar, and poo)
> and of course,affecting the whole row, so that "bar" should be on top,
> along with "foo" and "baz" as container[0].
>
> What method should i use?

Actually, the better way to sort in this scenario is to use sort_by.

container.sort_by {|x| x[1]}

No need to mess with the <=> operator by hand.

Brian.

Dominic Son

10/13/2006 11:03:00 PM

0

But how to do apply this to having 3 elements in the value of an array?

container.sort_by { |x,y,z| *lost here*
puts x <br>
puts y <br>
puts z <br>
}

Brian Mitchell wrote:
> On 10/13/06, Dominic Son <dominicson@gmail.com> wrote:
>> What method should i use?
> Actually, the better way to sort in this scenario is to use sort_by.
>
> container.sort_by {|x| x[1]}
>
> No need to mess with the <=> operator by hand.
>
> Brian.


--
Posted via http://www.ruby-....

Wilson Bilkovich

10/13/2006 11:11:00 PM

0

On 10/13/06, Dominic Son <dominicson@gmail.com> wrote:
> But how to do apply this to having 3 elements in the value of an array?
>
> container.sort_by { |x,y,z| *lost here*
> puts x <br>
> puts y <br>
> puts z <br>
> }

Having more entries in the array doesn't change the number of
arguments to the block.

a = []
a << [1,2,3]
a << [4,5,6]
a << [7,8,9]

a.sort_by do |element|
[element[0], element[1], element[2]]
end

If you return an Array from sort_by's block, it will sort on the
entries of the array in order.

In this trivial case, since you're sorting by all three, you could
just write the above as:
a.sort_by {|e| e}
(since 'e' is already an Array in the proper order)

x1

10/13/2006 11:13:00 PM

0

container.sort_by {|x| x[2]}

On 10/13/06, Dominic Son <dominicson@gmail.com> wrote:
> But how to do apply this to having 3 elements in the value of an array?
>
> container.sort_by { |x,y,z| *lost here*
> puts x <br>
> puts y <br>
> puts z <br>
> }
>
> Brian Mitchell wrote:
> > On 10/13/06, Dominic Son <dominicson@gmail.com> wrote:
> >> What method should i use?
> > Actually, the better way to sort in this scenario is to use sort_by.
> >
> > container.sort_by {|x| x[1]}
> >
> > No need to mess with the <=> operator by hand.
> >
> > Brian.
>
>
> --
> Posted via http://www.ruby-....
>
>