how to sort data according to date in python
Question: I have a input file in the following format:

    
    
    457526373620277249  17644162    Sat Apr 19 14:29:22 +0000 2014  0   nc  nc  U are expressing a wish not a fact ;) @Manicdj99 @ANTIVICTORIA @Nupe117 @cspanwj
    457522541926842368  402127017   Sat Apr 19 14:14:09 +0000 2014  0   nc  nc  @dfwlibrarian You're a great one to call somebody else "educationally challenged!" I'd call that a name call. #YouLose #PJNET #TCOT #TGDNGO YouLose,PJNET,TCOT,TGDNGO
    457519476511350786  65713724    Sat Apr 19 14:01:58 +0000 2014  0   nc  nc  @Manicdj99 @Nupe117 @cspanwj only some RW fringies are upset- &amp; they're ALWAYS angry at something-also too fat 2 get out of lazyboys
    

I need the data to be sorted according to time. I am using `strptime` function
but not able to sort the entire data according to time.

    
    
    import datetime
    dt=[]
    for line in f:
        splits = line.split('\t')
        dt.append(datetime.datetime.strptime(splits[2], "%a %b %d %H:%M:%S +0000 %Y"))
        dt.sort()
    


Answer: You'd want to produce a list of the lines, and only then sort the whole list;
you are only capturing the timestamps and are sorting that list each time you
add a new timestamp, ignoring the rest of your data.

You can more easily read the data using the [`csv`
module](https://docs.python.org/2/library/csv.html):

    
    
    import csv
    from datetime import datetime
    from operator import itemgetter
    
    rows = []
    with open(yourfile, 'rb') as f:
        reader = csv.reader(f, delimiter='\t')
        for row in reader:
            row[2] = datetime.strptime(row[2], "%a %b %d %H:%M:%S +0000 %Y")
            rows.append(row)
    
    rows.sort(key=itemgetter(2))  # sort by the datetime column
    

