Instead of doing it that way, consider using sort_by instead to sort
by the second element:

ruby-1.9.2-p180 :001 > fruits = [[:apple, 100], [:banana, 150],
[:tomato, 80], [:kumquat, 180]]
 => [[:apple, 100], [:banana, 150], [:tomato, 80], [:kumquat, 180]]

ruby-1.9.2-p180 :002 > fruits.sort_by { |f| f[1] }
 => [[:tomato, 80], [:apple, 100], [:banana, 150], [:kumquat, 180]]

~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/



On Thu, Apr 21, 2011 at 00:15, RichardOnRails
<RichardDummyMailbox58407 / uscomputergurus.com> wrote:
> Hi,
>
> Here an abstract version of my app, in which I have an array of two-
> element arrays I'd like to sort on the second element of each pair
> using the spaceship operator:
>
> $data = [ [:A, 12], [:B, 30], [:C, 4] ]
>
> Class MyTest < Array
>  def <=>(other)
>    self[1] <=> other[1]
>  end
>
>  @sorted_array = $date
> end
>
> How can I make this work?
>
> Thanks in advance,
> Richard
>
>