Sorting Domain Names By TLD

Suppose you have a list of domain names including subdomains, wouldn't it be nice to have those sorted by TLD first, followed by however deeply nested subdomain names?

If you happen to use PHP, things are simple (but not as short as in Perl, see link below):

<?php
$a = array('example.net'
          ,'www.example.org'
          ,'mail.example.org'
          ,'example.com'
          ,'ftp.example.com');
 
function reverse($s) {
  return implode(".", array_reverse(explode(".", $s)));
}
 
$b = array_map("reverse", $a);
sort($b);
$c = array_map("reverse", $b);
print_r($c);
?>

Result:

Array
(
  [0] => example.com
  [1] => ftp.example.com
  [2] => example.net
  [3] => mail.example.org
  [4] => www.example.org
)

(Inspiration from here: http://www.perlmonks.org/?node_id=299555)

 
notes/domainsort.txt ยท Last modified: 2020-02-25 00:19 by roland