Hi, I've a struct like this:
KK = Struct.new(:a,:b)
and some entries in an array:
kk1=KK.new(0,10)
#<struct KK a=0, b=10>
kk2=KK.new(0,5)
<struct KK a=0, b=5>
kk3=KK.new(2,0)
#<struct KK a=2, b=0>
kk4 = KK.new(2,5)
#<struct KK a=2, b=5>
array = [kk3, kk2, kk1, kk4]
I need to order the array based:
- Elements with minor :a must be first.
- If two elements have same :a, then order based on higher :b.
The result should be:
[kk1, kk2, kk4, kk3]
I expected that the following could work:
array.sort_by{|entry| entry.a or entry.b}
but it generates:
[kk1, kk2, kk3, kk4]
so obviously it does not work. I'm not very used to Enumerable.sort_by
method, any tip please?
Thanks a lot.
--
IƱaki Baz Castillo
<ibc / aliax.net>