title: Sorting items in a text file in python
body: <p>I've been trying to figure out how to sort results in a text file in order of test scores for a small task I'm doing using the lambda sort function. I'm unsure if this is the best way to do this or not but it was recommended to me by a friend.</p>
  
  <p>The results are entered into the text file like so:</p>
  
  <pre><code>    text_file = open("Results.txt", "a")
      text_file.write("\n")
      text_file.write(str(score))
      text_file.write(" ")
      text_file.write(userName)
      text_file.write(" ")
      text_file.write(userClass)
      text_file.close()
  </code></pre>
  
  <p>My current sorting system looks like this:</p>
  
  <pre><code>with open("Results.txt") as inf:
  data = []
  for line in inf:
      line = line.split()
      if len(line)==4:
          data.append(line)
  
  a = sorted(a, key=lambda x: x.modified, reverse=True)
  </code></pre>
  
  <p>And the text file looks like this:</p>
  
  <pre><code>5 Test b
  4 Test b
  6 Test c
  7 Test a
  </code></pre>
  
  <p>(score userName userClass)</p>
  
  <p>I would like to sort the results in descending order by score and I'm not sure this is the correct way to go about it.</p>
  
  <p>Thanks in advance.</p>