# we wish to sort the list of medalists, first by # medal placement, then alphabetically by name # we need a map in order to sort out the medal order my %map = ( gold => 1, silver => 2, bronze => 3 ); =for bad example # bad way - too much work being done in the sort! my @sorted = map { $_->[0] } sort { $map{$a->[2]} <=> $map{$b->[2]} or # very very lc($a->[1]) cmp lc($b->[1]) # evil!! } map { /^(([^:]+):\s+(\w+))$/ ? [$1,$2,$3] : () } ; =cut # good way - we only check the map hash once per item # and we only lc() the names once per item my @sorted = map { $_->[0] } sort { $a->[2] <=> $b->[2] or $a->[1] cmp $b->[1] } map { /^(([^:]+):\s+(\w+))$/ ? [$1, lc($2), $map{$3}] : () # very very good!! } ; print join("\n", @sorted); __DATA__ Sarah: silver Gloria: bronze Tracy: gold Shawn: silver Brad: gold Jack: bronze