Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于域对子域进行排序_C#_Jquery_Html - Fatal编程技术网

C# 基于域对子域进行排序

C# 基于域对子域进行排序,c#,jquery,html,C#,Jquery,Html,我有域和子域的列表,如 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#编程新手。有人能帮忙吗?可以这样做: List<string>

我有域和子域的列表,如

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#编程新手。有人能帮忙吗?

可以这样做:

        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);
            }
        }

您可能在对示例进行排序时出错,但这里有一个解决方案:

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]);
        }
    }
}
输出:

abc.com
help.abc.com
pages.abc.com
def.com
contactus.def.com
help.def.com
ijk.com
或者如果您没有数组,而是

List<string>
列表

IEnumerable
您可以使用Linq执行此操作:

IEnumerable<string> sorted = domains.OrderBy(x => x, new DomainComparer());
IEnumerable sorted=domains.OrderBy(x=>x,newdomaincomparer());

如果您只需要按二级域和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

如果您没有不同的域,则添加了特殊情况
IEnumerable<string>
IEnumerable<string> sorted = domains.OrderBy(x => x, new DomainComparer());
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));