1
0
Files
common/AMWD.Common/Comparer/DomainComparer.cs

58 lines
1.6 KiB
C#

using System.Collections.Generic;
namespace AMWD.Common.Comparer
{
/// <summary>
/// Defines a method to compare two domain strings.
/// </summary>
public class DomainComparer : IComparer<string>
{
/// <summary>
/// Compares two domain strings and returns a value indicating
/// whether one is less than, equal to, or greater than the other.
/// </summary>
/// <param name="x">The first domain string.</param>
/// <param name="y">The second domain string.</param>
/// <returns>
/// A signed integer that indicates the relative values of x and y, as shown in the following table:
/// <list type="table">
/// <listheader>
/// <term>Value</term>
/// <description>Meaning</description>
/// </listheader>
/// <item>
/// <term>Less than zero</term>
/// <description>x is less than y</description>
/// </item>
/// <item>
/// <term>Zero</term>
/// <description>x equals y</description>
/// </item>
/// <item>
/// <term>Greater than zero</term>
/// <description>x is greater than y</description>
/// </item>
/// </list>
/// </returns>
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;
}
}
}