Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use <a href="http://msdn.microsoft.com/en-us/library/bb534743%28v=vs.110%29.aspx" rel="nofollow"><code>ThenBy</code></a> and take the 2nd and 3rd children. But, that is not scalable, so it depends on the needs of the impl</p> <p>If you want something more robust, you could do the following. It will work for this specific case. I am going to see if I can optimize it to be more generic though :)</p> <pre><code>public static class myExt { public static List&lt;Parent&gt; OrderByWithTieBreaker(this List&lt;Parent&gt; parents, int depth = 0) { if (depth &gt; parents[0].Children.Count()) return parents; var returnedList = new List&lt;Parent&gt;(); Func&lt;Parent, int&gt; keySelector = x =&gt; { IEnumerable&lt;Child&gt; enumerable = x.Children.OrderBy(y =&gt; y.Age).Skip(depth); if (!enumerable.Any()) return 0; //If no children left, then return lowest possible age return enumerable.Min(z =&gt; z.Age); }; var orderedParents = parents.OrderBy(keySelector); var groupings = orderedParents.GroupBy(keySelector); foreach (var grouping in groupings) { if (grouping.Count() &gt; 1) { var innerOrder = grouping.ToList().OrderByWithTieBreaker(depth + 1); returnedList = returnedList.Union(innerOrder).ToList(); } else returnedList.Add(grouping.First()); } return returnedList; } } [TestFixture] public class TestClass { public class Parent { public string Name { get; set; } public List&lt;Child&gt; Children { get; set; } } public class Child { public int Age {get;set;} } [Test] public void TestName() { var parents = new List&lt;Parent&gt; { new Parent{Name="P3", Children = new List&lt;Child&gt;{new Child{Age=1}, new Child{Age=3}, new Child{Age=6}, new Child{Age=7}}}, new Parent{Name="P4", Children = new List&lt;Child&gt;{new Child{Age=1}, new Child{Age=3}, new Child{Age=6}, new Child{Age=7}}}, new Parent{Name="P2", Children = new List&lt;Child&gt;{new Child{Age=1}, new Child{Age=3}, new Child{Age=6}}}, new Parent{Name="P1", Children = new List&lt;Child&gt;{new Child{Age=1}, new Child{Age=2}, new Child{Age=7}}}, new Parent{Name="P5", Children = new List&lt;Child&gt;{new Child{Age=1}, new Child{Age=4}, new Child{Age=5}}} }; var f = parents.OrderByWithTieBreaker(); int count = 1; foreach (var d in f) { Assert.That(d.Name, Is.EqualTo("P"+count)); count++; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload