use strict; use warnings; my %res; while () { my ( $name, $weight ) = split /\s+/; push @{ $res{$name} }, $weight; } for ( sort { length($a) cmp length($b) } keys %res ) { print "$_ ", join( ",", @{ $res{$_} } ), "\n"; } for ( sort { $res{$a}->[0] <=> $res{$b}->[0] } keys %res ) { print "$_ ", join( ",", @{ $res{$_} } ), "\n"; } __DATA__ jim 14 john 23 ernest 38 matilda 43 jim 34 ernest 27 john 44 matilda 22