Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Consecutive sort on Array of Hashes (http://www.velocityreviews.com/forums/t832429-consecutive-sort-on-array-of-hashes.html)

 Laurent Colloud 08-14-2006 09:04 PM

Consecutive sort on Array of Hashes

Hi,

Here my - strange - problem.

To explain it, let's take the example of football. I construct an array
of hashes of the results with team_id, total of pts, number of wins,
number of draws and number of defeats such as:

myArray = Array.new
myArray << {:team_id=>1, :pts=>5, :w=>0, :d=>5, :l=>5}
myArray << {:team_id=>2, :pts=>7, :w=>1, :d=>4, :l=>5}
myArray << {:team_id=>3, :pts=>4, :w=>0, :d=>4, :l=>6}
myArray << {:team_id=>4, :pts=>6, :w=>1, :d=>3, :l=>6}
myArray << {:team_id=>5, :pts=>5, :w=>0, :d=>5, :l=>5}
myArray << {:team_id=>6, :pts=>5, :w=>0, :d=>5, :l=>5}
myArray << {:team_id=>8, :pts=>10, :w=>2, :d=>4, :l=>4}
myArray << {:team_id=>9, :pts=>5, :w=>1, :d=>2, :l=>7}
myArray << {:team_id=>10, :pts=>8, :w=>1, :d=>5, :l=>4}
myArray << {:team_id=>11, :pts=>9, :w=>2, :d=>3, :l=>5}
myArray << {:team_id=>12, :pts=>6, :w=>1, :d=>3, :l=>6}
myArray << {:team_id=>13, :pts=>5, :w=>0, :d=>5, :l=>5}

Now, from this array, I want to get the table.
So what I want to do is to sort the array, first by total of pts, then
by number of wins (if 2 teams have the same total of points I put first
the team with more wins) and then by number of draws.

That's how - logically - I would do it anyway. But that does not seem to
be the Ruby way. Doing it that way gives me a complete mess.

No worries, doing it the other way round works (first sort by draw, then
wins, then pts)! Here is what I run:

puts "Team, Pts, W, D, L"
myArray = myArray.sort { |a,b| b[:d] <=> a[:d]
}.sort { |a,b| b[:w] <=> a[:w]
}.sort { |a,b| b[:pts] <=> a[:pts]
}.each do |row|
puts "#{row[:team_id]}, #{row[:pts]}, #{row[:w]}, #{row[:d]},
#{row[:l]}"
end

Did I say it works? Well almost! Here is my output:

Team, Pts, W, D, L
8, 10, 2, 4, 4
11, 9, 2, 3, 5
10, 8, 1, 5, 4
2, 7, 1, 4, 5
4, 6, 1, 3, 6
12, 6, 1, 3, 6
16, 5, 0, 5, 5
1, 5, 0, 5, 5
9, 5, 1, 2, 7
5, 5, 0, 5, 5
6, 5, 0, 5, 5
3, 4, 0, 4, 6

Good news, we kept the correct team_is with the correct result.
But can anyone explain me what on earth is the line 9, 5, 1, 2, 7 doing
there in the middle?

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

 James Edward Gray II 08-14-2006 09:46 PM

Re: Consecutive sort on Array of Hashes

On Aug 14, 2006, at 4:04 PM, Laurent Colloud wrote:

> Hi,
>
> Here my - strange - problem.
>
> To explain it, let's take the example of football. I construct an
> array
> of hashes of the results with team_id, total of pts, number of wins,
> number of draws and number of defeats such as:
>
> myArray = Array.new
> myArray << {:team_id=>1, :pts=>5, :w=>0, :d=>5, :l=>5}
> myArray << {:team_id=>2, :pts=>7, :w=>1, :d=>4, :l=>5}
> myArray << {:team_id=>3, :pts=>4, :w=>0, :d=>4, :l=>6}
> myArray << {:team_id=>4, :pts=>6, :w=>1, :d=>3, :l=>6}
> myArray << {:team_id=>5, :pts=>5, :w=>0, :d=>5, :l=>5}
> myArray << {:team_id=>6, :pts=>5, :w=>0, :d=>5, :l=>5}
> myArray << {:team_id=>8, :pts=>10, :w=>2, :d=>4, :l=>4}
> myArray << {:team_id=>9, :pts=>5, :w=>1, :d=>2, :l=>7}
> myArray << {:team_id=>10, :pts=>8, :w=>1, :d=>5, :l=>4}
> myArray << {:team_id=>11, :pts=>9, :w=>2, :d=>3, :l=>5}
> myArray << {:team_id=>12, :pts=>6, :w=>1, :d=>3, :l=>6}
> myArray << {:team_id=>13, :pts=>5, :w=>0, :d=>5, :l=>5}
>
> Now, from this array, I want to get the table.
> So what I want to do is to sort the array, first by total of pts,
> then
> by number of wins (if 2 teams have the same total of points I put
> first
> the team with more wins) and then by number of draws.

Does this do what you were after?

\$ irb -r pp
>> pp myArray.sort_by { |team| [team[:pts], team[:w], team

[:d]] }.reverse
[{:l=>4, :team_id=>8, :pts=>10, :w=>2, :d=>4},
{:l=>5, :team_id=>11, :pts=>9, :w=>2, :d=>3},
{:l=>4, :team_id=>10, :pts=>8, :w=>1, :d=>5},
{:l=>5, :team_id=>2, :pts=>7, :w=>1, :d=>4},
{:l=>6, :team_id=>12, :pts=>6, :w=>1, :d=>3},
{:l=>6, :team_id=>4, :pts=>6, :w=>1, :d=>3},
{:l=>7, :team_id=>9, :pts=>5, :w=>1, :d=>2},
{:l=>5, :team_id=>13, :pts=>5, :w=>0, :d=>5},
{:l=>5, :team_id=>6, :pts=>5, :w=>0, :d=>5},
{:l=>5, :team_id=>5, :pts=>5, :w=>0, :d=>5},
{:l=>5, :team_id=>1, :pts=>5, :w=>0, :d=>5},
{:l=>6, :team_id=>3, :pts=>4, :w=>0, :d=>4}]
=> nil

Hope that helps.

James Edward Gray II

Re: Consecutive sort on Array of Hashes

> Hi,
>=20
> Here my - strange - problem.
>=20
> To explain it, let's take the example of football. I=20
> construct an array=20
> of hashes of the results with team_id, total of pts, number of wins,=20
> number of draws and number of defeats such as:
>=20
> myArray =3D Array.new
> myArray << {:team_id=3D>1, :pts=3D>5, :w=3D>0, :d=3D>5, :l=3D>5}
> myArray << {:team_id=3D>2, :pts=3D>7, :w=3D>1, :d=3D>4, :l=3D>5}
> myArray << {:team_id=3D>3, :pts=3D>4, :w=3D>0, :d=3D>4, :l=3D>6}
> myArray << {:team_id=3D>4, :pts=3D>6, :w=3D>1, :d=3D>3, :l=3D>6}
> myArray << {:team_id=3D>5, :pts=3D>5, :w=3D>0, :d=3D>5, :l=3D>5}
> myArray << {:team_id=3D>6, :pts=3D>5, :w=3D>0, :d=3D>5, :l=3D>5}
> myArray << {:team_id=3D>8, :pts=3D>10, :w=3D>2, :d=3D>4, :l=3D>4}
> myArray << {:team_id=3D>9, :pts=3D>5, :w=3D>1, :d=3D>2, :l=3D>7}
> myArray << {:team_id=3D>10, :pts=3D>8, :w=3D>1, :d=3D>5, :l=3D>4}
> myArray << {:team_id=3D>11, :pts=3D>9, :w=3D>2, :d=3D>3, :l=3D>5}
> myArray << {:team_id=3D>12, :pts=3D>6, :w=3D>1, :d=3D>3, :l=3D>6}
> myArray << {:team_id=3D>13, :pts=3D>5, :w=3D>0, :d=3D>5, :l=3D>5}
>=20
> Now, from this array, I want to get the table.
> So what I want to do is to sort the array, first by total of=20
> pts, then=20
> by number of wins (if 2 teams have the same total of points I=20
> put first=20
> the team with more wins) and then by number of draws.
>=20
> That's how - logically - I would do it anyway. But that does=20
> not seem to=20
> be the Ruby way. Doing it that way gives me a complete mess.
>=20
> No worries, doing it the other way round works (first sort by=20
> draw, then=20
> wins, then pts)! Here is what I run:
>=20
> puts "Team, Pts, W, D, L"
> myArray =3D myArray.sort { |a,b| b[:d] <=3D> a[:d]
> }.sort { |a,b| b[:w] <=3D> a[:w]
> }.sort { |a,b| b[:pts] <=3D> a[:pts]
> }.each do |row|
> puts "#{row[:team_id]}, #{row[:pts]}, #{row[:w]}, #{row[:d]},=20
> #{row[:l]}"
> end
>=20
> Did I say it works? Well almost! Here is my output:
>=20
> Team, Pts, W, D, L
> 8, 10, 2, 4, 4
> 11, 9, 2, 3, 5
> 10, 8, 1, 5, 4
> 2, 7, 1, 4, 5
> 4, 6, 1, 3, 6
> 12, 6, 1, 3, 6
> 16, 5, 0, 5, 5
> 1, 5, 0, 5, 5
> 9, 5, 1, 2, 7
> 5, 5, 0, 5, 5
> 6, 5, 0, 5, 5
> 3, 4, 0, 4, 6
>=20
> Good news, we kept the correct team_is with the correct result.
> But can anyone explain me what on earth is the line 9, 5, 1,=20
> 2, 7 doing=20
> there in the middle?
>=20

Each of your sort's just re-arranges the array by the new criteria,
completely unrelated to previous sorts. Why not simply to create an
appropriate criteria for a single sort:

[ DISCLAIMER: Untested code bellow ]

myArray =3D myArray.sort { |b, a|=20
diff =3D 0
[ :pts, :w, :d ].each do |_criteria|
diff =3D a[_criteria] - b[_criteria]
break unless diff.zero?
end
diff
}

Or even cooler ;-)

myArray =3D myArray.sort { |a, b|
[ :pts, :w, :d ].inject(0) { |_m, _c|
_m =3D=3D 0 ? b[_c] - a[_c] : _m
}
}

>=20
> --=20
> Posted via http://www.ruby-forum.com/.
>=20
>=20

 Ken Bloom 08-15-2006 03:54 AM

Re: Consecutive sort on Array of Hashes

On Tue, 15 Aug 2006 07:02:52 +0900, Gennady Bystritsky wrote:

>> Hi,
>>
>> Here my - strange - problem.
>>
>> To explain it, let's take the example of football. I
>> construct an array
>> of hashes of the results with team_id, total of pts, number of wins,
>> number of draws and number of defeats such as:
>>
>> myArray = Array.new
>> myArray << {:team_id=>1, :pts=>5, :w=>0, :d=>5, :l=>5}
>> myArray << {:team_id=>2, :pts=>7, :w=>1, :d=>4, :l=>5}
>> myArray << {:team_id=>3, :pts=>4, :w=>0, :d=>4, :l=>6}
>> myArray << {:team_id=>4, :pts=>6, :w=>1, :d=>3, :l=>6}
>> myArray << {:team_id=>5, :pts=>5, :w=>0, :d=>5, :l=>5}
>> myArray << {:team_id=>6, :pts=>5, :w=>0, :d=>5, :l=>5}
>> myArray << {:team_id=>8, :pts=>10, :w=>2, :d=>4, :l=>4}
>> myArray << {:team_id=>9, :pts=>5, :w=>1, :d=>2, :l=>7}
>> myArray << {:team_id=>10, :pts=>8, :w=>1, :d=>5, :l=>4}
>> myArray << {:team_id=>11, :pts=>9, :w=>2, :d=>3, :l=>5}
>> myArray << {:team_id=>12, :pts=>6, :w=>1, :d=>3, :l=>6}
>> myArray << {:team_id=>13, :pts=>5, :w=>0, :d=>5, :l=>5}
>>
>> Now, from this array, I want to get the table.
>> So what I want to do is to sort the array, first by total of
>> pts, then
>> by number of wins (if 2 teams have the same total of points I
>> put first
>> the team with more wins) and then by number of draws.
>>
>> That's how - logically - I would do it anyway. But that does
>> not seem to
>> be the Ruby way. Doing it that way gives me a complete mess.
>>
>> No worries, doing it the other way round works (first sort by
>> draw, then
>> wins, then pts)! Here is what I run:
>>
>> puts "Team, Pts, W, D, L"
>> myArray = myArray.sort { |a,b| b[:d] <=> a[:d]
>> }.sort { |a,b| b[:w] <=> a[:w]
>> }.sort { |a,b| b[:pts] <=> a[:pts]
>> }.each do |row|
>> puts "#{row[:team_id]}, #{row[:pts]}, #{row[:w]}, #{row[:d]},
>> #{row[:l]}"
>> end
>>
>> Did I say it works? Well almost! Here is my output:
>>
>> Team, Pts, W, D, L
>> 8, 10, 2, 4, 4
>> 11, 9, 2, 3, 5
>> 10, 8, 1, 5, 4
>> 2, 7, 1, 4, 5
>> 4, 6, 1, 3, 6
>> 12, 6, 1, 3, 6
>> 16, 5, 0, 5, 5
>> 1, 5, 0, 5, 5
>> 9, 5, 1, 2, 7
>> 5, 5, 0, 5, 5
>> 6, 5, 0, 5, 5
>> 3, 4, 0, 4, 6
>>
>> Good news, we kept the correct team_is with the correct result.
>> But can anyone explain me what on earth is the line 9, 5, 1,
>> 2, 7 doing
>> there in the middle?
>>

>
> Each of your sort's just re-arranges the array by the new criteria,
> completely unrelated to previous sorts. Why not simply to create an
> appropriate criteria for a single sort:

Not of .sort is a stable sort. Then it will preserve the order of items
that are equal according to the current comparison. You would sort from
least significant to most significant exactly as he has done here.

From the results though, I conclude that .sort isn't a stable sort.

Looking through the C code, I see that .sort is a quicksort.

http://en.wikipedia.org/wiki/Sorting...ing_algorithms

--Ken

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

 Kroeger, Simon (ext) 08-15-2006 09:25 AM

Re: Consecutive sort on Array of Hashes

Hi,=20

> From: Laurent Colloud
> Sent: Monday, August 14, 2006 11:05 PM
>=20
> Hi,
>=20
> Here my - strange - problem.
>=20
> To explain it, let's take the example of football. I=20
> construct an array=20
> of hashes of the results with team_id, total of pts, number of wins,=20
> number of draws and number of defeats such as:
> [...]
>=20
> puts "Team, Pts, W, D, L"
> myArray =3D myArray.sort { |a,b| b[:d] <=3D> a[:d]
> }.sort { |a,b| b[:w] <=3D> a[:w]
> }.sort { |a,b| b[:pts] <=3D> a[:pts]
> }.each do |row|
> puts "#{row[:team_id]}, #{row[:pts]}, #{row[:w]}, #{row[:d]},=20
> #{row[:l]}"
> end

try

myArray.sort {|a,b|=20
(b[:d] <=3D> a[:d]).nonzero? ||
(b[:w] <=3D> a[:w]).nonzero? ||
(b[:pts] <=3D> a[:pts])}

or (even better):

myArray.sort_by {|a| [a[:d], a[:w], a[:pts]]}

cheers

Simon

 Laurent Colloud 08-15-2006 09:48 PM

Re: Consecutive sort on Array of Hashes

Hi guys,

First thank you very much for helping me out on that one.

So here are the results of all your suggestions. And the winner is...

-----------------------------------
THEY WORK
-----------------------------------

By James:

myArray.sort_by { |team| [team[:pts], team[:w], team[:d]] }.reverse

myArray.sort { |b, a|
diff = 0
[ :pts, :w, :d ].each do |_criteria|
diff = a[_criteria] - b[_criteria]
break unless diff.zero?
end
diff
}

myArray.sort { |a, b|
[ :pts, :w, :d ].inject(0) { |_m, _c|
_m == 0 ? b[_c] - a[_c] : _m
}
}

By Russell (with a few synthax corrections):

myArray.sort {|a,b|
if b[:pts] == a[:pts]
if b[:w] == a[:w]
b[:d] <=> a[:d]
else
b[:w] <=> a[:w]
end
else
b[:pts] <=> a[:pts]
end
}

-----------------------------------------------
THEY DON'T WORK
-----------------------------------------------

myArray.sort_by {|a| [a[:d], a[:w], a[:pts]]}

-> Sorted by draws (:d) ASC (and then :w ASC, :pts ASC) but I could
deduct James'solution from the output of this so thanks anyway :).

myArray.sort {|a,b|
(b[:d] <=> a[:d]).nonzero? ||
(b[:w] <=> a[:w]).nonzero? ||
(b[:pts] <=> a[:pts])}

->Sorted by :d DESC (and then :w DESC, :pts DESC)

So thanks again very much.

Pfiouuu, I've learned a lot of Ruby in just one night! :)

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

 All times are GMT. The time now is 10:59 PM.