Sorting domain names

Has anyone done a sorting of the domain name list?

I've seen some applications sort them as flat strings, but the problem is that you end up scattering all related nodes in the domain:

a.me.com a.you.com b.me.com b.you.com

So in the main logic, I paid attention to the order of the labels, then I sort.

The FQDNs of a single label should be treated as hostnames and probably sorted separately, possibly at the top.

Ideally I am looking for javascript and java options.

I also don't know if this project is a good fit for new internationalized domain names.

+1


source to share


6 answers


Based on Tom's answer ...


hosts = new Array( "a.foo.com", "b.foo.com", "foo.com", "c.bar.com" );

//print("Unsorted:");
//for (host in hosts )
//     print(hosts[host]);

sorted_hosts = new Array();
split_hosts = new Array();

for(h in hosts)
{
    segments = hosts[h].split('.');
    segments.reverse(); 
    split_hosts.push(segments);
}

split_hosts.sort()

for(h in split_hosts)
{
    split_hosts[h].reverse()
    sorted_hosts.push(split_hosts[h].join("."))
}

//print("Sorted:");
//for (host in sorted_hosts )
//     print(sorted_hosts[host]);

      



The print statements work (when uncommented) in the SquareFree JavaScript Development Environment , a convenient place to test javascript snippets ...

+2


source


I don't know about Java and Javascript in particular, but many languages ​​provide some sort of array data structure that can be lexicographically sorted. So, as you said, convert "a.example.com" to {"com", "example", "a"} and just allow the default collation. Then the lexicographic look will do exactly what you want.

If you have a list of local domains as well as FQDNs, I agree that you want to separate them. Anything that doesn't have a period in it can be filtered out first. Or you can allow it all for the FQDN and then just sort the entire list.

Some Python code that does this (should be close enough to Javascript):



hosts = ["a.foo.com", "b.foo.com", "foo.com", "c.bar.com"]

split_hosts = []
for h in hosts:
    segments = h.split('.')
    segments.reverse()
    split_hosts.append(segments)

split_hosts.sort()
for segments in split_hosts:
    segments.reverse()
    print ".".join(segments)

      

Prints:

c.bar.com
foo.com
a.foo.com
b.foo.com

      

+1


source


This is how it is done in Perl:

#!/usr/bin/perl -w
use strict;

my @hosts = qw(
 bar.org
 a.foo.com
 b.foo.com
 foo.com
 c.bar.com
);

print join("\n", sort {
 $a = lc($a);
 $b = lc($b);
 if ($a eq $b) {
  return 0;
 }
 my @a = reverse(split(/\./, $a));
 my @b = reverse(split(/\./, $b));
 my $max = (scalar(@a), scalar(@b))[@a < @b];
 for (my $i=0; $i < $max; $i++) {
  if (($i < @a) && ($i < @b)) {
   if (my $c = $a[$i] cmp $b[$i]) {
    return $c;
   }
  }
  else {
   return scalar(@a) <=> scalar(@b);
  }
 }
 return 0;
} @hosts) . "\n";

      

+1


source


You can split domain names into separate fields and do sequential sorts. You can create a domain name object to have three fields and create a list of domain names to sort. Sort for each of the three fields. At the end, you have a list of sorted domain names with related hosts together.

0


source


This is the result of the big entian war against the small end of the 80s, which was won by the small party team. In the UK, domain names were originally ordered as (hypothetical) "uk.ac.leeds" for the British Academic University of Leeds. These are large orders - and makes your choice a lot easier. It would also be much more difficult to spoof internet sites in URLs. Of course, the order is of little importance at this time, and the hypothetical URL would be "leeds.ac.uk".

To sort related domain names together intelligently, you would need to sort by best-fit component (.com, .uk, .org) first, then the next one on the left and repeat ... In other words (as @Bala said), you would need to do something similar to splitting names upwards and sorting from right to left.

0


source


I came up with this solution which uses the Array.prototype.sort () and ES6 generators:

function* reverseIterateParts(domain) {
    let currentEnd = domain.length;
    for (let index = currentEnd-1; index >= -1; index--) {
        if (index == -1 || domain[index] == '.') {
            yield domain.substring(index + 1, currentEnd);
            currentEnd = index;
        }
    }
}

arrayOfDomainNames.sort((domainA, domainB) => {
    let partsOfA = reverseIterateParts(domainA);
    let partsOfB = reverseIterateParts(domainB);
    while (true) {
        let partA = partsOfA.next();
        let partB = partsOfB.next();

        if (partA.done) {
            if (partB.done) {
                return 0;
            }
            return -1;
        }
        if (partB.done) {
            return 1;
        }
        if (partA.value > partB.value) {
            return 1;
        }
        if (partA.value < partB.value) {
            return -1;
        }
    }
});

      

0


source







All Articles