You can read your data into a list, with each row being a string in the list. Then sort the list with a key function that only looks at the mmdd
part of the date.
Here's some code that illustrates the idea using a hard-coded list, but it should be easy for you to adapt it to read the lines from your file.
data = '''
19500101 20.7
19500102 19.9
19500103 -77.1
19500104 -1.2
19510101 230.1
19520101 -91.8
19530101 20.0
'''.splitlines()[1:]
def keyfunc(line):
return line.split(None, 1)[0][4:]
data.sort(key=keyfunc)
for row in data:
print row
output
19500101 20.7
19510101 230.1
19520101 -91.8
19530101 20.0
19500102 19.9
19500103 -77.1
19500104 -1.2
Here's a fancier key function:
def keyfunc(line):
date = line.split(None,1)[0]
return date[4:], date[:4]
If two items have the same mmdd
they are then compared on the yyyy
, so that all items with the same mmdd
are grouped together but within the group they'll also be sorted by year.
The line.split(None,1)[0]
gets the date portion of the line. You could just use line.split()[0]
to do the same thing, but that's less efficient, since it has to split the whole line into individual columns, and we only need the first column for our key.