我有域名和子域列表,如
abc.com
def.com
ijk.com
pages.abc.com
help.abc.com
contactus.def.com
help.def.com
我的要求是按域排序此列表,以便最终输出为
abc.com
pages.abc.com
help.abc.com
def.com
contactus.def.com
ijk.com
我如何在C#中实现这一目标?我是C#编程的新手。有人可以帮忙吗?
答案 0 :(得分:1)
认为您在对示例进行排序时出错,但这是一个解决方案:
class DomainComparer : IComparer<string>
{
public int Compare(string x, string y)
{
if(x==y) return 0;
string[] _x = x.Split('.');
string[] _y = y.Split('.');
return Compare(_x, _y, 0);
}
private int Compare(string[] x, string[] y, int depth)
{
if (x.Length - depth - 1 >= 0 && y.Length - depth -1 < 0)
{
return +1;
}
if (y.Length - depth - 1 >= 0 && x.Length - depth -1 < 0)
{
return -1;
}
if (x[x.Length-depth-1].CompareTo(y[y.Length - depth-1]) == 0)
{
return Compare(x, y, depth + 1);
}
else
{
return x[x.Length-depth-1].CompareTo(y[y.Length - depth-1]);
}
}
}
然后你可以用:
来调用它string[] domains = new[] { "abc.com", "def.com", "ijk.com", "pages.abc.com", "help.abc.com", "contactus.def.com", "help.def.com" };
Array.Sort(domains, new DomainComparer());
foreach (var item in domains)
{
Console.WriteLine(item);
}
输出:
abc.com
help.abc.com
pages.abc.com
def.com
contactus.def.com
help.def.com
ijk.com
或者如果你没有数组但是
List<string>
或
IEnumerable<string>
你可以用Linq做到这一点:
IEnumerable<string> sorted = domains.OrderBy(x => x, new DomainComparer());
答案 1 :(得分:1)
如果您只需按二级域名排序&amp; TLD然后你可以做这样的事情。
var uriList = new string[] {"abc.com", "def.com", "ijk.com", "pages.abc.com",
"help.abc.com", "contactus.def.com", "help.def.com"};
var query = from uri in uriList.Select(item =>
new { Uri = item, UriParts = item.Split('.') })
orderby uri.UriParts[uri.UriParts.Length-2] + uri.UriParts[uri.UriParts.Length-1],
string.Join(".", uri.UriParts) select uri.Uri;
Console.WriteLine(string.Join(" ,", query));
输出与您期望的略有不同,如下所示。
abc.com,help.abc.com,pages.abc.com,contactus.def.com,def.com,help.def.com,ijk.com
答案 2 :(得分:0)
可以这样做:
List<string> list = new List<string>();
list.Add("abc.com");
list.Add("def.com");
list.Add("ijk.com");
list.Add("pages.abc.com");
list.Add("help.abc.com");
list.Add("contactus.def.com");
list.Add("help.def.com");
List<string> listRoot = new List<string>();
List<string> listSub = new List<string>();
foreach (string item in list)
{
string[] split = item.Split(new char[] {'.'}, StringSplitOptions.RemoveEmptyEntries);
if (split.Length == 2)
{
listRoot.Add(item);
}
else
{
listSub.Add(item);
}
}
listRoot.Sort();
Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
foreach (string root in listRoot)
{
List<string> listSubIntern = new List<string>();
foreach (string item in listSub)
{
if (item.EndsWith(root, StringComparison.InvariantCultureIgnoreCase))
{
listSubIntern.Add(item);
}
}
listSubIntern.Sort();
dictionary.Add(root, listSubIntern);
}
foreach (KeyValuePair<string, List<string>> keyValuePair in dictionary)
{
Console.WriteLine(keyValuePair.Key);
foreach (string s in keyValuePair.Value)
{
Console.WriteLine("\t{0}", s);
}
}
和输出:
abc.com
help.abc.com
pages.abc.com
def.com
contactus.def.com
help.def.com
ijk.com