http://www.perlmonks.org?node_id=299555
Description:

Ever had an alphabetical (or not) list of domain names:

de.yahoo.com mail.example.org www.aol.com www.example.org www.freshmeat.net www.hotdoggie.com.au

... and wished it was sorted by tld (and recursively down the subdomains)?

www.hotdoggie.com.au www.aol.com de.yahoo.com www.freshmeat.net mail.example.org www.example.org

From the shell, it's a snap: perl -ple'$_=join".",reverse split/\./' reverse the domain components (mail.example.org becomes org.example.mail). This can then be piped to another filter (e.g. sort) and then the same filter can be run again to undo the reversal.

It's even easier if the command is wrapped up in an alias. (And yes, I know about "Useless use of cat(1)". I'm using it here to show the symmetry of the munging/demunging).

cat foo | perl -ple'$_=join".",reverse split/\./' | sort | \
          perl -ple'$_=join".",reverse split/\./'
# or
alias tldmunge='perl -ple'"'"'$_=join".",reverse split/\./'"'"
cat foo | tldmunge | sort | tldmunge