problem sorting

A

Augusto Garcia

I am trying to sort an array using a block but somehow I am not seeing
what I am want. First I want to sort the total, then break the tie on
gold goldcount, silvercount and finally on bronzcount

Here is my code. what am I missing? please help

# compare and order by totals
result = b[:total] <=> a[:total]
# if tied, break the number of gold medals
result != 0 ? result : b[:goldcount] <=> a[:goldcount]
result != 0 ? result : b[:silvercount] <=> a[:silvercount]
result != 0 ? result : b[:bronzcount] <=> a[:bronzcount]
result != 0 ? result : b[:country] <=> a[:country]
end
 
J

Jesús Gabriel y Galán

I am trying to sort an array using a block but somehow I am not seeing
what I am want. First I want to sort the total, then break the tie on
gold goldcount, silvercount and finally on bronzcount

Here is my code. what am I missing? please help

=A0# compare and order by totals
=A0 =A0 =A0 =A0result =3D b[:total] <=3D> a[:total]
=A0 =A0 =A0 =A0# if tied, break the number of gold medals
=A0 =A0 =A0 =A0result !=3D 0 ? result : b[:goldcount] <=3D> a[:goldcount]
=A0 =A0 =A0 =A0result !=3D 0 ? result : b[:silvercount] <=3D> a[:silverco= unt]
=A0 =A0 =A0 =A0result !=3D 0 ? result : b[:bronzcount] <=3D> a[:bronzcoun= t]
=A0 =A0 =A0 =A0result !=3D 0 ? result : b[:country] <=3D> a[:country]
=A0end

I assume you have an array of hashes:

irb(main):001:0> country_medals =3D []
=3D> []

irb(main):002:0> country_medals << {:country =3D> "Spain", :total =3D>
100, :goldcount =3D> 100, :silvercount =3D> 0, :bronzecount =3D> 0}
=3D> [{:country=3D>"Spain", :total=3D>100, :goldcount=3D>100, :silvercount=
=3D>0,
:bronzecount=3D>0}]

irb(main):003:0> country_medals << {:country =3D> "US", :total =3D> 50,
:goldcount =3D> 50, :silvercount =3D> 0, :bronzecount =3D> 0}
=3D> [{:country=3D>"Spain", :total=3D>100, :goldcount=3D>100, :silvercount=
=3D>0,
:bronzecount=3D>0}, {:country=3D>"US", :total=3D>50, :goldcount=3D>50,
:silvercount=3D>0, :bronzecount=3D>0}]

irb(main):004:0> country_medals << {:country =3D> "UK", :total =3D> 50,
:goldcount =3D> 45, :silvercount =3D> 5, :bronzecount =3D> 0}
=3D> [{:country=3D>"Spain", :total=3D>100, :goldcount=3D>100, :silvercount=
=3D>0,
:bronzecount=3D>0}, {:country=3D>"US", :total=3D>50, :goldcount=3D>50,
:silvercount=3D>0, :bronzecount=3D>0}, {:country=3D>"UK", :total=3D>50,
:goldcount=3D>45, :silvercount=3D>5, :bronzecount=3D>0}]

irb(main):010:0> country_medals.sort_by {|x| [-x[:total],
-x[:goldcount], -x[:silvercount], -x[:bronzecount], x[:country]]}
=3D> [{:country=3D>"Spain", :total=3D>100, :goldcount=3D>100, :silvercount=
=3D>0,
:bronzecount=3D>0}, {:country=3D>"US", :total=3D>50, :goldcount=3D>50,
:silvercount=3D>0, :bronzecount=3D>0}, {:country=3D>"UK", :total=3D>50,
:goldcount=3D>45, :silvercount=3D>5, :bronzecount=3D>0}]


Jesus.
 
R

Rob Biedenharn

Since no one has duplicated what you seemed to be trying:

I am trying to sort an array using a block but somehow I am not =20
seeing
what I am want. First I want to sort the total, then break the tie on
gold goldcount, silvercount and finally on bronzcount

Here is my code. what am I missing? please help

# compare and order by totals
result =3D b[:total] <=3D> a[:total]
# if tied, break the number of gold medals
result !=3D 0 ? result : b[:goldcount] <=3D> a[:goldcount]
result !=3D 0 ? result : b[:silvercount] <=3D> a[:silvercount]
result !=3D 0 ? result : b[:bronzcount] <=3D> a[:bronzcount]
result !=3D 0 ? result : b[:country] <=3D> a[:country]
end

.sort {|a,b|
(b[:total] <=3D> a[:total]).nonzero? ||
(b[:goldcount] <=3D> a[:goldcount]).nonzero? ||
(b[:silvercount] <=3D> a[:silvercount]).nonzero? ||
(b[:bronzcount] <=3D> a[:bronzcount]).nonzero? ||
b[:country] <=3D> a[:country]
}

Look at what Numeric#nonzero? is meant for ;-)

Also, your original breaks the final tie by reverse sort of the =20
country so I do here as well. Augusto has an ascending sort on all and =20=

Jes=FAs just has an ascending sort on the country.

-Rob
I assume you have an array of hashes:

irb(main):001:0> country_medals =3D []
=3D> []

irb(main):002:0> country_medals << {:country =3D> "Spain", :total =3D>
100, :goldcount =3D> 100, :silvercount =3D> 0, :bronzecount =3D> 0}
=3D> [{:country=3D>"Spain", :total=3D>100, :goldcount=3D>100, = :silvercount=3D>0,
:bronzecount=3D>0}]

irb(main):003:0> country_medals << {:country =3D> "US", :total =3D> = 50,
:goldcount =3D> 50, :silvercount =3D> 0, :bronzecount =3D> 0}
=3D> [{:country=3D>"Spain", :total=3D>100, :goldcount=3D>100, = :silvercount=3D>0,
:bronzecount=3D>0}, {:country=3D>"US", :total=3D>50, :goldcount=3D>50,
:silvercount=3D>0, :bronzecount=3D>0}]

irb(main):004:0> country_medals << {:country =3D> "UK", :total =3D> = 50,
:goldcount =3D> 45, :silvercount =3D> 5, :bronzecount =3D> 0}
=3D> [{:country=3D>"Spain", :total=3D>100, :goldcount=3D>100, = :silvercount=3D>0,
:bronzecount=3D>0}, {:country=3D>"US", :total=3D>50, :goldcount=3D>50,
:silvercount=3D>0, :bronzecount=3D>0}, {:country=3D>"UK", :total=3D>50,
:goldcount=3D>45, :silvercount=3D>5, :bronzecount=3D>0}]

irb(main):010:0> country_medals.sort_by {|x| [-x[:total],
-x[:goldcount], -x[:silvercount], -x[:bronzecount], x[:country]]}
=3D> [{:country=3D>"Spain", :total=3D>100, :goldcount=3D>100, = :silvercount=3D>0,
:bronzecount=3D>0}, {:country=3D>"US", :total=3D>50, :goldcount=3D>50,
:silvercount=3D>0, :bronzecount=3D>0}, {:country=3D>"UK", :total=3D>50,
:goldcount=3D>45, :silvercount=3D>5, :bronzecount=3D>0}]


Jesus.

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top