c# - 通过查看每个对象自己的路径对多个对象集合进行排序

标签 c# sorting loops collections reference

这是我的代码问题,它让我很痛苦。我不知道该怎么做……我发现自己创建了临时数组来保存引用、循环中的循环……一团糟。

设置

看图。我们有 3 个 Something 集合(层),集合之间有交叉引用。

目标是按箭头指示订购 Somethings。但是,C 比 B 更重要,B 比 A 更重要(这是自下而上的重要性)。一个 Something 可以通过在不同“层”中跟随另一个 Something 来改变自然顺序。

Imgur

所以期望的顺序是:

+----+----+----+---+---+---+---+---+----+----+----+---+---+---+---+
| 14 | 15 | 17 | 7 | 8 | 1 | 2 | 9 | 18 | 19 | 12 | 3 | 4 | 5 | 6 |
+----+----+----+---+---+---+---+---+----+----+----+---+---+---+---+

您可能需要多看几次才能得出相同的结论。让我为您分解一下。

我们从 C 层开始(C 高于 B 和 A,还记得吗?)...左边的第一列是起始列。 我们有 14、15 和 17。我们不想要 18,因为 18 没有返回到 17 的引用。所以我们向上移动一层并再次从 7 和 8 开始。它在那里结束,因为 9 有不是引用 8。

我们再往上一层,得到1和2。

然后就变得困难了 -- 2 之后是 9,所以我们先得到 9。然后我们看到 9 后面跟着 18,所以我们捕获那个。 这里是重要的部分 -- C 的排名高于 B 和 A,所以我们首先得到 19,然后上升到 12,然后我们回到 A 层继续在 2 之后得到 3,4, 5,6。

必须有一些巧妙的方法可以做到这一点并且仍然保持快速。这是一个精简的例子。真实的东西有几十个对象和图层。

真实的东西有一个虚拟的属性来完成一对多的关系。我想避免使用该属性,因为它会向混合中添加另一个集合,但如果它能让事情变得更容易,我会在此处添加它。

class Something
{
  public int Id {get;set;}
  public int FollowsId {get;set;}
  public IEnumerable<Something> FollowedBy {get;set;}
}

我重命名了这些属性,以便更容易掌握。

最佳答案

您可以将其视为树结构,然后从左到右遍历它(或者在您的情况下,从子层直接链接的节点先于同一层的节点)确保在生成当前节点之前导航子节点...

工作代码:

public class Layer
{
    public string Name { get; set; }
    public int Priority { get; set; }

    public Something Head { get; set; }
    public Something Add(Something s) { 
        if (this.Head == null) this.Head = s; 
        s.Layer = this; 
        this.Items.Add(s); 
        return s; 
    }
    public Something this[int id] { 
        get { return this.Items.SingleOrDefault(s => s.Id == id); } 
    }
    public List<Something> Items = new List<Something>();

    private void BuildTree(List<Something> list, Something s = null)
    {
        list.Add(s);
        foreach(Something ss in s.Followers.OrderBy(sss => sss.Layer.Priority))
        {
            BuildTree(list, ss);
        }
    }
    public List<Something> Tree
    {
        get 
        {
            List<Something> list = new List<Something>();
            if (this.Head != null) BuildTree(list, this.Head);
            return list;
        }
    }
}

public class Something
{
    public int Id { get; set; }
    public Layer Layer { get; set; }
    public List<Something> Followers = new List<Something>();
    public void Follows(Something s) { s.Followers.Add(this); }
}

void Main()
{
    Layer A = new Layer() { 
        Name="A", 
        Priority = 3
    };
    A.Add(new Something() { Id = 1 });
    A.Add(new Something() { Id = 2 }).Follows(A[1]);
    A.Add(new Something() { Id = 3 }).Follows(A[2]);
    A.Add(new Something() { Id = 4 }).Follows(A[3]);
    A.Add(new Something() { Id = 5 }).Follows(A[4]);
    A.Add(new Something() { Id = 6 }).Follows(A[5]);

    Layer B = new Layer() { 
        Name = "B",
        Priority = 2
    };
    B.Add(new Something() { Id = 7 });
    B.Add(new Something() { Id = 8 }).Follows(B[7]);
    B.Add(new Something() { Id = 9 }).Follows(A[2]);
    B.Add(new Something() { Id = 12 }).Follows(B[9]);

    Layer C = new Layer() {
        Name = "C",
        Priority = 1
    };
    C.Add(new Something() { Id = 14 });
    C.Add(new Something() { Id = 15 }).Follows(C[14]);
    C.Add(new Something() { Id = 17 }).Follows(C[15]);
    C.Add(new Something() { Id = 18 }).Follows(B[9]);
    C.Add(new Something() { Id = 19 }).Follows(C[18]);

    List<Something> orderedItems = new List<Something>();
    List<Layer> layers = new List<Layer>() { A, B, C };
    foreach(Layer l in layers.OrderBy(ll => ll.Priority)) orderedItems.AddRange(l.Tree);
}

如果您在 LinqPad 中运行它,那么在最后一行之后,您可以:

string.Join(",", orderedItems.Select(s => s.Id.ToString())).Dump();

查看输出:

14,15,17,7,8,1,2,9,18,19,12,3,4,5,6

关于c# - 通过查看每个对象自己的路径对多个对象集合进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32100012/

相关文章:

Java增强循环

c# - 后台消费者线程生命周期管理最佳实践

c# - PDF表: last cell is not visible

c# - 在 C# 中获取动态类型的最大值

algorithm - 有没有办法使用 Foldr 或 Foldl 函数在 SML 中编写堆排序算法?

c++ - 嵌入式 Boost::Python 和 C++:并行运行

javascript - 在对象内部迭代对象

c# - Monodevelop 4.0 无代码查看

javascript - 不一致的 Javascript 排序

javascript - 找到数组中最短元素的第一个实例