How to sort depended objects by dependency

后端 未结 10 569
青春惊慌失措
青春惊慌失措 2020-11-28 19:31

I have a collection:

List> dependencyHierarchy;

The first item in pair is some object (item) and the

10条回答
  •  天命终不由人
    2020-11-28 19:52

    I would make this easier on myself by storing the dependencies of an Item within the Item itself:

    public class Item
    {
        private List m_Dependencies = new List();
    
        protected AddDependency(Item _item) { m_Dependencies.Add(_item); }
    
        public Item()
        {
        }; // eo ctor
    
        public List Dependencies {get{return(m_Dependencies);};}
    } // eo class Item
    

    Then, given this you can implement a custom Sort delegate for List that sorts based on whether the given Item is contained within the other's list of dependencies:

    int CompareItem(Item _1, Item _2)
    {
        if(_2.Dependencies.Contains(_1))
            return(-1);
        else if(_1.Dependencies.Contains(_2))
            return(1);
        else
            return(0);
    }
    

提交回复
热议问题