#! perl -slw use strict; my @un_sorted = ([3,4,23,4,5],[3,4,56,2,4],[2,3,43,5,3],[2,3,43,6],[3,4]); print join "\n", map{ join ',', @$_ } sort { for( 0 .. ( $#{$a} < $#{$b} ? $#{$b} : $#{$a} ) ) { return ( $a->[$_] || 0 ) <=> ( $b->[$_] || 0 ) || next } } @un_sorted; __END__ P:\test>before Use of uninitialized value in numeric comparison (<=>) at P:\test\junk.pl line 10. 2,3,43,5,3 2,3,43,6 3,4 3,4,23,4,5 3,4,56,2,4 P:\test>after 2,3,43,5,3 2,3,43,6 3,4 3,4,23,4,5 3,4,56,2,4