title: Sort a number by its digits
body: <p>I have to sort a vector of integers (all integers have the same length). Integers with the same first digit must be sorted in relation to the second digits, and numbers with the same: first and second digits are sorted by third digit etc. Also, the subsequent digits are sorted alternately (once ascending and once descending)</p>
  <p>So when I have <code>lis = [137, 944, 972, 978, 986]</code>,
  I should get <code>sorted_lis = [137, 986, 972, 978, 944]</code></p>
  <p>I know how to sort by selecting digit (a)</p>
  <pre><code>lis.sort(key=lambda x: int(str(x)[a]))
  </code></pre>
  <p>I've tried using insertion sort, (since I have to use a stable sorting algorithm)</p>
  <pre><code>def insertion_sort(list):
      for i in range(len(list)):
          key = list[i]
          j = i-1
          while j &gt;= 0 and key &lt; list[j]:
              list[j+1] = list[j]
              j -= 1
          list[j+1] = key
  
      return list
  </code></pre>