title: Advanced array sorting/re-arranging in Java
body: <p>So I have an array with the following theoretical values:</p>
  
  <pre><code>int[] elements = {A1, A2, B1, B2,
                    A3, A4, B3, B4,
                    C1, C2, D1, D2,
                    C3, C4, D3, D4};
  </code></pre>
  
  <p>Illustrative figure:</p>
  
  <pre><code>                  + - + - + - + - +
                    | A | A | B | B |
                    + - + - + - + - +
                    | A | A | B | B |
                    + - + - + - + - +
                    | C | C | D | D |
                    + - + - + - + - +
                    | C | C | D | D |
                    + - + - + - + - +
  </code></pre>
  
  <p>Simply put I would like the array to be re-arranged to the following form:</p>
  
  <pre><code>int[] elements = {A1, A2, A3, A4,
                    B1, B2, B3, B4,
                    C1, C2, C3, C4,
                    D1, D2, D3, D4};
  </code></pre>
  
  <p>Illustrative figure:</p>
  
  <pre><code>                  + - + - + - + - +
                    | A | A | A | A |
                    + - + - + - + - +
                    | B | B | B | B |
                    + - + - + - + - +
                    | C | C | C | C |
                    + - + - + - + - +
                    | D | D | D | D |
                    + - + - + - + - +
  </code></pre>
  
  <p>This particular example contains four sectors (A, B, C and D), but the algorithm I need should work however many or few sectors the array contains, and however many elements each sector contains.</p>
  
  <p>The size of each sector is known (sector width and sector height) and also the amount of sectors (rows and columns). <em>All sectors are of the exact same size (width and height).</em> The amount of sectors has to be described as two values (rows ans columns) which is then multiplied to make up the actual sum of sectors. Eg. if 5 sectors are wanted, then 1 row and  5 columns could be specified.</p>
  
  <p>Here follows an example of what a method preforming this sort could look like:</p>
  
  <pre><code>public int[] sectorSort(int[] elements,
                          int sectorWidth,
                          int sectorHeight,
                          int columns,
                          int rows);
  </code></pre>
  
  <p>Example of other sector setups:</p>
  
  <pre><code>                  Columns: 5
                    + - + - + - + - + - + - + - + - + - + - +
                    | A | A | B | B | C | C | D | D | E | E |
       Rows: 1      + - + - + - + - + - + - + - + - + - + - +
                    | A | A | B | B | C | C | D | D | E | E |
                    + - + - + - + - + - + - + - + - + - + - +
  
                    Columns: 2
                    + - + - + - + - +
                    | A | A | B | B |
                    + - + - + - + - +
                    | A | A | B | B |
                    + - + - + - + - +
                    | C | C | D | D |
       Rows: 3      + - + - + - + - +
                    | C | C | D | D |
                    + - + - + - + - +
                    | E | E | F | F |
                    + - + - + - + - +
                    | E | E | F | F |
                    + - + - + - + - +
  </code></pre>
  
  <p>I plan on using this to make an efficient sprite map class for a game engine I'm making. The elements in the array are ARGB color values, and the sectors are individual sprites. If the different sprites are arranged in the latter order then searching for individual sprites goes a lot faster and memory efficient.</p>
  
  <p>Thank you!</p>
  
  <p>EDIT1: Clarity.</p>
  
  <p>EDIT2: Added more conditions and clarifications.</p>