User Tools

Site Tools


notes:domainsort

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)

Comments

Enter your comment:
 
  • Bookmark "Sorting Domain Names By TLD" at Google
  • Bookmark "Sorting Domain Names By TLD" at Facebook
  • Bookmark "Sorting Domain Names By TLD" at Twitter
notes/domainsort.txt · Last modified: 2013-03-03 12:30 (external edit)