如何在C#中订购此站点URL列表?

时间:2013-06-14 16:04:00

标签: c# linq

我有一个网站网址列表,

  • /node1
  • /node1/sub-node1
  • /node2
  • /node2/sub-node1

列表是以随机顺序提供给我的,我需要订购它以便首先是顶级,然后是子级别等等(因为我不能创建/node2/sub-node1而没有{{1}现有的)。有干净的方法吗?

现在我正在进行递归调用,如果由于/node2存在而无法创建sub-node1,请创建node2。我希望列表的顺序决定创建并摆脱我的递归调用。

7 个答案:

答案 0 :(得分:5)

我的第一个想法是按字符串的长度排序......但后来我想到了这样的列表,可能包括类似短名称的别名:

/longsitename/  
/a  
/a/b/c/  
/a  
/a/b/  
/otherlongsitename/  

...我认为更好的选择是首先按级别分隔符的数量排序:

IEnumerable<string> SortURLs(IEnumerable<string> urls)
{
    return urls.OrderBy(s => s.Count(c => c == '/')).ThenBy(s => s);
}

然后我又考虑了一下,我在你的问题中看到了这一行:

  

如果没有/ node2现有

,我无法创建/ node2 / sub-node1

啊哈!只要孩子总是在父母之后列出,那么部分或部分内的顺序并不重要。考虑到这一点,我原来的想法是可以的,单独按字符串长度排序应该没问题:

IEnumerable<string> SortURLs(IEnumerable<string> urls)
{
    return urls.OrderBy(s => s.Length);
}

这让我终于想知道为什么我关心它的长度?如果我只是对字符串进行排序,无论长度如何,具有相同开头的字符串将始终先对较短的字符串进行排序。因此,最后:

IEnumerable<string> SortURLs(IEnumerable<string> urls)
{
    return urls.OrderBy(s => s);
}

我将保留第一个样本,因为如果在将来的某个时候,您需要更多的词法或逻辑排序顺序,这可能会有用。

答案 1 :(得分:2)

  

有干净的方法吗?

使用标准字符串排序对URI列表进行排序可以满足您的需求。通常,“a”将在字符串排序中的“aa”之前排序,因此“/ node1”应该在“/ node1 / sub-node”之前结束。

例如:

List<string> test = new List<string> { "/node1/sub-node1", "/node2/sub-node1", "/node1",  "/node2"  };

foreach(var uri in test.OrderBy(s => s))
   Console.WriteLine(uri);

这将打印:

/node1
/node1/sub-node1
/node2
/node2/sub-node1

答案 2 :(得分:2)

也许这适合你:

var nodes = new[] { "/node1", "/node1/sub-node1", "/node2", "/node2/sub-node1" };
var orderedNodes = nodes
    .Select(n => new { Levels = Path.GetFullPath(n).Split('\\').Length, Node = n })
    .OrderBy(p => p.Levels).ThenBy(p => p.Node);

结果:

foreach(var nodeInfo in orderedNodes)
{
    Console.WriteLine("Path:{0} Depth:{1}", nodeInfo.Node, nodeInfo.Levels);
}

Path:/node1 Depth:2
Path:/node2 Depth:2
Path:/node1/sub-node1 Depth:3
Path:/node2/sub-node1 Depth:3

答案 3 :(得分:0)

如果您的意思是在所有第二级节点之前需要所有第一级节点,请按斜杠数/排序:

string[] array = {"/node1","/node1/sub-node1", "/node2", "/node2/sub-node1"};

array = array.OrderBy(s => s.Count(c => c == '/')).ToArray();

foreach(string s in array)
    System.Console.WriteLine(s);

结果:

/node1
/node2
/node1/sub-node1
/node2/sub-node1

如果你只需要子节点之前的父节点,它就不会比

简单得多
Array.Sort(array);

结果:

/node1
/node1/sub-node1
/node2
/node2/sub-node1

答案 4 :(得分:0)

var values = new string[]{"/node1", "/node1/sub-node1" ,"/node2", "/node2/sub-node1"};
foreach(var val in values.OrderBy(e => e))
{
    Console.WriteLine(val);
}

答案 5 :(得分:0)

最好是使用自然排序,因为你的字符串在字符串和数字之间混合。因为如果您使用其他排序方法或技术,并且您有这样的例子:

List<string> test = new List<string> { "/node1/sub-node1" ,"/node13","/node10","/node2/sub-node1", "/node1", "/node2" };

输出将是:

/node1
/node1/sub-node1
/node10
/node13
/node2
/node2/sub-node1

未分类。

您可以查看此Implementation

答案 6 :(得分:0)

递归实际上就是你应该使用的,因为这最容易用树结构来表示。

public class PathNode {
    public readonly string Name;
    private readonly IDictionary<string, PathNode> _children;

    public PathNode(string name) {
        Name = name;
        _children = new Dictionary<string, PathNode>(StringComparer.InvariantCultureIgnoreCase);
    }

    public PathNode AddChild(string name) {
        PathNode child;

        if (_children.TryGetValue(name, out child)) {
            return child;
        }

        child = new PathNode(name);

        _children.Add(name, child);

        return child;
    }

    public void Traverse(Action<PathNode> action) {
        action(this);

        foreach (var pathNode in _children.OrderBy(kvp => kvp.Key)) {
            pathNode.Value.Traverse(action);
        }
    }
}

然后你可以这样使用:

var root = new PathNode(String.Empty);

var links = new[] { "/node1/sub-node1", "/node1", "/node2/sub-node-2", "/node2", "/node2/sub-node-1" };

foreach (var link in links) {
    if (String.IsNullOrWhiteSpace(link)) {
        continue;
    }

    var node = root;

    var lastIndex = link.IndexOf("/", StringComparison.InvariantCultureIgnoreCase);

    if (lastIndex < 0) {
        node.AddChild(link);
        continue;
    }

    while (lastIndex >= 0) {
        lastIndex = link.IndexOf("/", lastIndex + 1, StringComparison.InvariantCultureIgnoreCase);

        node = node.AddChild(lastIndex > 0 
            ? link.Substring(0, lastIndex) // Still inside the link 
            : link // No more slashies
        );
    }
}

var orderedLinks = new List<string>();

root.Traverse(pn => orderedLinks.Add(pn.Name));

foreach (var orderedLink in orderedLinks.Where(l => !String.IsNullOrWhiteSpace(l))) {
    Console.Out.WriteLine(orderedLink);
}

哪个应该打印:

/node1
/node1/sub-node1
/node2
/node2/sub-node-1
/node2/sub-node-2