in reply to How to sort?

With sort, of course ;-)

#!/usr/bin/perl use strict; use warnings; my @names; while (<DATA>){ chomp; m/^(\d+)\s+(.*)/; push @names, [$1, $2]; } @names = sort { $a->[1] cmp $b->[1] } @names; for (@names){ print "$_->[0] $_->[1]\n"; } __DATA__ 1 Charlie 1 Edward 2 Bob 2 Alfred 3 Barry

Replies are listed 'Best First'.
Re^2: How to sort?
by merlyn (Sage) on Oct 14, 2007 at 22:31 UTC
    m/^(\d+)\s+(.*)/; push @names, [$1, $2];
    Don't use $1 without testing whether the match succeeded or not. If you expect the match to always succeed, at least add an "or die". Failure to do so will give you a "stale" $1 when you least expect it.
Re^2: How to sort?
by redss (Monk) on Oct 14, 2007 at 21:25 UTC
    works great, Thanks!