Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Help with efficient (elegant) ordering

$
0
0

Hi,

So I have a class ClassMate:

public class ClassMate
{
   public Guid Id { get; set; }
   public List<Guid> Depends { get; set; }

public ClassMate()
{
Id = Guid.NewGuid();
Depends = new List<Guid>();
} }


I fill a list of ClassMates like this:

var mate1 = new ClassMate();
var mate2 = new ClassMate(); var mate3 = new ClassMate();
var mate4 = new ClassMate();
mate2.Depends.Add(mate4.Id);mate3.Depends.Add(mate2.Id);
mate1.Depends.Add(mate3.Id);
mate1.Depends.Add(mate4.Id);

var list = new List<ClassMate>(); list.Add(mate1); list.Add(mate2); list.Add(mate3); list.Add(mate4);

Now I need to order them in a new list, so that no mate's index is lower than one of its dependant. In other words, what I want is a list that looks like this:

list[0] = mate4
list[1] = mate2
list[2] = mate3
list[3] = mate1

Can you suggest an elegant way to sort like this?

-- 
Werner



Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>