using System.Collections.Generic;
namespace AMWD.Common.Comparer
{
///
/// Defines a method to compare two domain strings.
///
public class DomainComparer : IComparer
{
///
/// Compares two domain strings and returns a value indicating
/// whether one is less than, equal to, or greater than the other.
///
/// The first domain string.
/// The second domain string.
///
/// A signed integer that indicates the relative values of x and y, as shown in the following table:
///
///
/// Value
/// Meaning
///
/// -
/// Less than zero
/// x is less than y
///
/// -
/// Zero
/// x equals y
///
/// -
/// Greater than zero
/// x is greater than y
///
///
///
public int Compare(string x, string y)
{
string[] left = x.Split('.');
string[] right = y.Split('.');
int result = left.Length.CompareTo(right.Length);
if (result != 0)
return result;
// Compare from TLD to subdomain with string comparison
for (int i = left.Length - 1; i >= 0; i--)
{
result = left[i].CompareTo(right[i]);
if (result != 0)
return result;
}
return 0;
}
}
}