Sorting Hash of teams for premier league table

E

Eireann Kelly

This is what the hash looks like (with only 4 teams in it) ignore the
Competition:

{
'Team' => [
{
'draws' => '0',
'goal_difference' => '-1',
'wins' => '1',
'name' => 'Fulham',
'points' => '3',
'goals_conceded' => '5',
'losses' => 0,
'goals' => '4',
'games_played' => '3'
},
{
'draws' => '0',
'goal_difference' => '-9',
'wins' => 0,
'name' => 'Wolverhampton Wanderers',
'points' => '0',
'goals_conceded' => '10',
'losses' => '3',
'goals' => '1',
'games_played' => '3'
},
{
'draws' => '1',
'goal_difference' => '5',
'wins' => '2',
'name' => 'Portsmouth',
'points' => '7',
'goals_conceded' => '2',
'losses' => 0,
'goals' => '7',
'games_played' => '3'
},
{
'draws' => '0',
'goal_difference' => '6',
'wins' => '3',
'name' => 'Manchester United',
'points' => '9',
'goals_conceded' => '1',
'losses' => 0,
'goals' => '7',
'games_played' => '3'
},
],
'Competition' => [
{
'uID' => '8',
'Name' => 'English Barclaycard
Premiership',
'Season' => [
{
'uID' => '4',
'StartDate' =>
'2003-06-20 00:00:00',
'Name' => 'Season
2003/2004',
'EndDate' =>
'2004-06-19 00:00:00'
}
]
}
]
}

Ok i need to either insert a stat position which sorts on points, then
goal_difference, then goals, then goals_conceded (ascending), Or just
somehow sort the hash by those keys, using the sort function and
keeping it in that format. I've never done this before so would be
very grateful for some help.....
 
T

Tad McClellan

Eireann Kelly said:
This is what the hash looks like (with only 4 teams in it) ignore the
Competition:


Why not snip out the Competition rather than include it then
tell us to ignore it?

Ok i need to either insert a stat position which sorts on points, then


You should have some data where the points are equal, else you
won't be able to see the "then" part working...

goal_difference, then goals, then goals_conceded (ascending), Or just
somehow sort the hash by those keys, using the sort function and
keeping it in that format.


Assuming that $h is a reference to your hash:

foreach my $hash ( sort by_multiple @{$h->{Team}} ) {
print Dumper $hash;
}

sub by_multiple {
$a->{points} <=> $b->{points} or
$a->{goal_difference} <=> $b->{goal_difference} or
$a->{goals} <=> $b->{goals} or
$a->{goals_conceded} <=> $b->{goals_conceded}
}
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top