# a sample file, lets say a *really* long /etc/passwd file # with lines like root:x:0:0:Super User:/:/sbin/sh someone:x:45:14:Some One:/home/someone:/bin/csh sometwo:x:46:14:Some Two:/home/sometwo:/bin/ksh # Lets say im gonna sort on uid then gid, I'll do while () { chomp; ($uid,$gid) = ( split(/:/) )[2,3]; push(@tosort, [$uid, $gid, $_]); } ... for ( sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @tosort ) { print "$_->[2]\n"; }