title: Sort on last element of inconsistent number of elements list
body: <p><strong>Introduction</strong></p>
  
  <p>Follow up to <a href="https://stackoverflow.com/questions/25159935/unable-to-sort-multiple-lines-on-last-string-of-line">this question</a>.</p>
  
  <p><strong>Aim:</strong> sort on last element of inconsistent number of elements list</p>
  
  <p><strong>Methods</strong></p>
  
  <p><em>Code</em></p>
  
  <pre><code>import os
  
  cwd = os.getcwd()
  
  for fn in os.listdir(cwd):
      print(fn)
  
      with open(fn, "r+") as f, open(cwd + "\\all.txt", "r+") as f2:
          lines = sorted(f, key=lambda text: text.split('-', 1)[::-1])
          print(lines)
  
          my_new_list = [fn + " - " + x for x in lines]
          print (my_new_list)
  
          f2.write("\n".join(my_new_list))
  </code></pre>
  
  <p><em>Sample</em></p>
  
  <pre><code>aa - http://cc
  bb - http://bb
  cc - http://aa
  aa - aa - http://cc
  bb - bb - http://bb
  cc - cc - http://aa
  aa - aa - aa - http://cc
  bb - bb - bb - http://bb
  cc - cc - cc - http://aa
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <p>The list is not sorted on the last element. It seems that the sorting took place on the second element.</p>
  
  <p><em>Current</em></p>
  
  <pre><code>sample.txt - aa - aa - aa - http://cc
  
  sample.txt - aa - aa - http://cc
  
  sample.txt - bb - bb - bb - http://bb
  
  sample.txt - bb - bb - http://bb
  
  sample.txt - cc - cc - cc - http://aa
  sample.txt - cc - cc - http://aa
  
  sample.txt - cc - http://aa
  
  sample.txt - bb - http://bb
  
  sample.txt - aa - http://cc
  </code></pre>
  
  <p><em>Expected</em></p>
  
  <pre><code>sample.txt - cc - http://aa
  sample.txt - cc - cc - http://aa
  sample.txt - cc - cc - cc - http://aa
  
  sample.txt - bb - http://bb
  sample.txt - bb - bb - http://bb
  sample.txt - bb - bb - bb - http://bb
  
  sample.txt - aa - http://cc
  sample.txt - aa - aa - http://cc
  sample.txt - aa - aa - aa - http://cc
  </code></pre>