-1

I've got a list of different types that I've read from a spreadsheet.

>>>data
[1.0, u'1', u'COMPLETED', u'TONGUE', 30.0, 1.0, '2014/01/28 09:33:16', 30.0, 210.0, u'1 CW', 0.0, 2.0, 210.0, u'H1490']
[1.0, u'1', u'COMPLETED', u'TONGUE', 30.0, 1.0, '04/02/2014 09:14:36', 30.0, 230.0, u'2 CCW', 0.0, 7.0, 210.0, u'H990']
.......

and I would like to be sorted by date, i.e. sorted by the 6th field (data[1][6],data[2][6],etc...). How could I indicate Python to sort this list by field number 6?

Manolete
  • 3,431
  • 7
  • 54
  • 92
  • pass key use a lambda to sorted or sort – Jason Hu Aug 22 '14 at 15:16
  • @Owen a zipped list is nothing similar to what I am asking – Manolete Aug 22 '14 at 15:18
  • 3
    This is very difficult without a consistent date format. Is "04/02/2014" later or earlier than "05/01/2014"? Depends on whether the latter is May 1st 2014 or the 5th of January 2014. – Kevin Aug 22 '14 at 15:21
  • you are going to need to turn your date `str` into something that will sort correctly, like a `datetime`. With inconsistent date format maybe look at `dateutil` parser. – cmd Aug 22 '14 at 15:23
  • @cmd What if I have all dates in the same format? %Y/%m/%d %H:%M:%S ? Strings could also be sorted right? – Manolete Aug 22 '14 at 15:25
  • 1
    YYYYMMDD style dates can be sorted lexicographically, so in that case you don't need to do any parsing at all. – Kevin Aug 22 '14 at 15:28
  • 1
    yes `str` can be sorted lexicographically as Kevin said, but if its not in most significant element order and each element `0` padded to the same number of digits, it will not turn out how you want. – cmd Aug 22 '14 at 15:32
  • @cmd Yes, I've noticed. I think I would have less control on sorting operations – Manolete Aug 22 '14 at 15:34

2 Answers2

3

You can use sorted and specify that you want to sort by 6th field.

You can use dateutil.parser to parse the date:

import dateutil.parser
sorted(data, key=lambda x: dateutil.parser.parse(x[6]))

Or if you want to sort data in-place and do less look-ups:

from dateutil.parser import parse
data.sort(key=lambda x: parse(x[6]))
FredrikHedman
  • 1,223
  • 7
  • 14
jh314
  • 27,144
  • 16
  • 62
  • 82
1

Use sorted using a lambda as a key to sort and dateutil:

from dateutil import parser

sorted(data,key = lambda x:parser.parse(x[6]))

If you don't have dateutil installed:

pip install python-dateutil

If you want to sort the list in place and avoid creating a new list:

data.sort(key=lambda x:parser.parse(x[6]))

If the dates are in one of two different formats you can use your own function:

def date_parse(x):
    date = None
    for form in ["%Y/%m/%d %H:%M:%S","%d/%m/%Y %H:%M:%S"]:
        try:
            date = datetime.strptime(x,form)
        except ValueError:
            pass
    return date

sorted(data,key = lambda x: date_parse(x[6]))
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321