my @nodes = ( "abc.subdomain1.wasweissich.de","def.subdomain1.another.org","ghi.wasweissich.de","abc.subdomain2.wasweissich.de","def.subdomain1.wasweissich.de","ghi.subdomain2.a nother.org","xyz.another.org" ); my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, uc( (/\w+\.(\w+\.\w+)/)[0]) ] } @nodes; print "$_\n" foreach @sorted;