my @nodes = ( "abc.subdomain1.wasweissich.de","def.subdomain1.another.org","ghi.wasweissich.de","abc.subdomain2.wasweissich.de","def.subdomain1.wasweissich.de","ghi.subdomain2.another.org","xyz.another.org" ); my @sorted = map { $_->[0] } sort { compare( $a->[1], $b->[1] ) } map { [ $_, [ reverse( split /\./, $_ ) ] ] } @nodes; print "$_\n" foreach @sorted; sub compare { my( $x, $y ) = @_; for my $i ( 0 .. ( @$x > @$y ? @$x : @$y ) ) { my $c = $x->[$i] cmp $y->[$i]; return $c unless $c == 0; } return 1 if @$x > @$y; return -1 if @$y > @$x; return 0; }