title: Python - How to sort a text file based on date time stamp on a column
body: <p>I have a text file below:</p>
  
  <pre><code>asd|2016-03-17 14:33:05|asd|
  asd|2017-03-17 14:27:25|asd|
  asd|2016-03-15 14:27:25|asd|
  asd|2016-03-17 14:33:05|asd|
  asd|2016-03-16 14:33:05|asd|
  </code></pre>
  
  <p>I tried the code below but I am getting "IndexError: string index out of range" error. </p>
  
  <pre><code>with open(file) as inf:
      for line in inf:
          data = line.split('|')
          sorted_data = sorted(data, key=lambda line: line[1])
  </code></pre>
  
  <p>Desired Output: I want to sort the text file using the second column delimited by '|' and output it to another file.</p>