title: How to arrange / sort the elements of an array such that it matches a specific pattern?
body: <h2>PROBLEM SETUP</h2>
  <p>I am using the following code to generate an array that contains arrays which have a given length and whose elements' sums equal to some given value (Note that repetition of digits is allowed):-</p>
  <pre><code>from itertools import product
    
  def find_combinations(sum_value, len_value):
      lst = range(sum_value + 1)
  
      return [
          pair 
          for pair in product(lst, repeat=len_value) 
          if sum(pair) == sum_value
      ]
  
  # ----------------------------------------- #
  
  sum_value = # some value
  len_value = # some value
  
  print(find_combinations(sum_value, len_value))
  </code></pre>
  <p>For example,</p>
  <p>If the sum, <code>sum_value</code>, is <code>4</code>, and the length, <code>len_value</code>, is <code>2</code>, the output this code produces is:-</p>
  <pre><code>[
      (0, 4),
      (1, 3),
      (2, 2),
      (3, 1),
      (4, 0),
  ]
  </code></pre>
  <hr />
  <h2>THE ACTUAL PROBLEM</h2>
  <p>This code works perfectly - it contains all the combinations that I was expecting. <strong>But the problem here is that the order of the array is incorrect</strong>.</p>
  <p>That is,</p>
  <p>In the previous example, where the required <code>sum</code> was <code>4</code>, and the <code>length</code> was <code>2</code>,
  the output should have been this:-</p>
  <pre><code>[
      (4, 0),
      (0, 4),
      (3, 1),
      (1, 3),
      (2, 2),
  ]
  </code></pre>
  <p>So if the required <code>sum</code> is <code>4</code>, and the <code>length</code> is <code>3</code>, the output that this code produces is:-</p>
  <pre><code>[
      (0, 0, 4),
      (0, 1, 3),
      (0, 2, 2),
      (0, 3, 1),
      (0, 4, 0),
      (1, 0, 3),
      (1, 1, 2),
      (1, 2, 1),
      (1, 3, 0),
      (2, 0, 2),
      (2, 1, 1),
      (2, 2, 0),
      (3, 0, 1),
      (3, 1, 0),
      (4, 0, 0),
  ]
  </code></pre>
  <p>But the order of the expected output is:-</p>
  <pre><code>[
      (4, 0, 0),
      (0, 4, 0),
      (0, 0, 4),
      (3, 1, 0),
      (0, 3, 1),
      (1, 0, 3),
      (1, 3, 0),
      (0, 1, 3),
      (3, 0, 1),
      (2, 1, 1),
      ...,
  ]
  </code></pre>
  <p>So my question is, how can I arrange the elements of this array such that it matches this pattern?</p>
  <hr />
  <p>P.S. - You can refer to the <a href="https://stackoverflow.com/q/72238215/17898569"><img src="https://img.shields.io/badge/original_question-grey" alt="Generic badge" /></a>, from which this one is inspired.</p>