Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I re-order a sorted List of ints into batches so that each batch has ints that are as far apart as possible from each other?
    primarykey
    data
    text
    <p>I have a sorted <code>List</code> of <code>int</code>s (in descending order) that I need to re-order so that each batch (e.g. in 20s) has <code>int</code>s that are as far apart as possible from each other.</p> <p>So, for example, if the list contains the following bookings:</p> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> <li>11</li> <li>12</li> </ul> <p>and I was to process them in a batch of 4, would a good approach be to split the bookings into 4 lists (12 / 3) and moving down each list of 4 take 1 from each and append to the newly created re-ordered list? So, the new list would contain the bookings in the following order:</p> <ul> <li>1</li> <li>5</li> <li>9</li> <li>2</li> <li>6</li> <li>10</li> <li>3</li> <li>7</li> <li>11</li> <li>4</li> <li>8</li> <li>12</li> </ul> <p>I appreciate it's not easy to understand what I'm after with my question so it might be easier to look at my code! I came up with the following approach and was wondering if it is the best/most performant way of achieving what I'm after:</p> <pre><code>var bookings = new List&lt;int&gt;(); // &lt;snip&gt;import bookings ints from csv file into bookings list&lt;/snip&gt; bookings = bookings.Distinct().OrderByDescending(x =&gt; x).ToList(); int chunkSize = 20; // get number of chunks/batches int chunkCount = (int)Math.Ceiling((double)bookings.Count/(double)chunkSize); // split booking list into number of chunks // add the ith booking from each chunk into new list // ToChunks() extension method from http://stackoverflow.com/a/6852288/1578713 var chunkList = bookings.ToChunks(chunkCount).ToList(); var reorderedBookings = new List&lt;int&gt;(); for (int i=0; i&lt;chunkCount; i++) { // last chunk may be smaller than chunkSize hence the check reorderedBookings.AddRange(from chunk in chunkList where chunk.Count() &gt; i select chunk.ToList()[i]); } bookings = reorderedBookings; </code></pre> <h3>The Background (if you really want to know)</h3> <p>My problem is that I'm processing* a <code>List</code> of <code>int</code>s (bookings that are imported from a csv file and ordered) in parallel batches (of 20, say) and the closer together (more consecutive) these bookings are in the list the more likely it is that an exception will be thrown (beyond my control).</p> <p>It's a tradeoff between running thousands of bookings synchronously (so no exception will be thrown) or running 20-odd in parallel with a likelihood of the said exception being thrown every once in a while. I've opted for the latter approach because it's quicker and any bookings that result in exceptions can be run synchronously after the async tasks have completed.</p> <p><sub>*calling a couple of services, passing in each booking <code>int</code> in turn</sub></p>
    singulars
    1. This table or related slice is empty.
    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. COWhen looking at, "how far apart the values are from each other" is it the max - min value, the average distance between each of the elements, what? If you've already written the function, could you provide the code for how you determine the "score" for a particular batch?
      singulars
    2. CO@Servy, appreciate your comments. I was using the more literal meaning of shuffle admittedly. I'm not sure I understand what you mean by your second comment as I've provided all the relevant code?
      singulars
    3. COYou have demonstrated how to batch the sequence (not super well, but get it working before making it pretty). You say you want "an order so that each batch has values that are as far apart as possible from each other." I'm saying I don't know what that means. How does one come up with that value for a given batch? Once you have that function we can address the more complex issues of maximizing that value. We also need to know how to aggregate that value for each batch. Do we want one batch with the greatest value, maximize the average value between batches, etc.
      singulars
 

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