Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Something like this?</p> <pre><code>public IEnumerable&lt;int&gt; SortToMiddle(IEnumerable&lt;int&gt; input) { var sorted = new List&lt;int&gt;(input); sorted.Sort(); var firstHalf = new List&lt;int&gt;(); var secondHalf = new List&lt;int&gt;(); var sendToFirst = true; foreach (var current in sorted) { if (sendToFirst) { firstHalf.Add(current); } else { secondHalf.Add(current); } sendToFirst = !sendToFirst; } //to get the highest values on the outside just reverse //the first list instead of the second secondHalf.Reverse(); return firstHalf.Concat(secondHalf); } </code></pre> <p>For your specific (general) case (assuming unique keys):</p> <pre><code>public static IEnumerable&lt;T&gt; SortToMiddle&lt;T, TU&gt;(IEnumerable&lt;T&gt; input, Func&lt;T, TU&gt; getSortKey) { var sorted = new List&lt;TU&gt;(input.Select(getSortKey)); sorted.Sort(); var firstHalf = new List&lt;TU&gt;(); var secondHalf = new List&lt;TU&gt;(); var sendToFirst = true; foreach (var current in sorted) { if (sendToFirst) { firstHalf.Add(current); } else { secondHalf.Add(current); } sendToFirst = !sendToFirst; } //to get the highest values on the outside just reverse //the first list instead of the second secondHalf.Reverse(); sorted = new List&lt;TU&gt;(firstHalf.Concat(secondHalf)); //This assumes the sort keys are unique - if not, the implementation //needs to use a SortedList&lt;TU, T&gt; return sorted.Select(s =&gt; input.First(t =&gt; s.Equals(getSortKey(t)))); } </code></pre> <p>And assuming non-unique keys:</p> <pre><code>public static IEnumerable&lt;T&gt; SortToMiddle&lt;T, TU&gt;(IEnumerable&lt;T&gt; input, Func&lt;T, TU&gt; getSortKey) { var sendToFirst = true; var sorted = new SortedList&lt;TU, T&gt;(input.ToDictionary(getSortKey, t =&gt; t)); var firstHalf = new SortedList&lt;TU, T&gt;(); var secondHalf = new SortedList&lt;TU, T&gt;(); foreach (var current in sorted) { if (sendToFirst) { firstHalf.Add(current.Key, current.Value); } else { secondHalf.Add(current.Key, current.Value); } sendToFirst = !sendToFirst; } //to get the highest values on the outside just reverse //the first list instead of the second secondHalf.Reverse(); return(firstHalf.Concat(secondHalf)).Select(kvp =&gt; kvp.Value); } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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