views:

41

answers:

3

I start out with date strings:

from operator import itemgetter
import datetime as DT

# unsorted dates
raw = (map(int, "2010-08-01".split("-")),
       map(int, "2010-03-25".split("-")),
       map(int, "2010-07-01".split("-")))

transactions = []
for year, month, day in raw:
    new = (DT.date(year, month, day), "Some data here")
    transactions.append(new)
# transactions is now a list with tuples nested inside, for example
# [(date, "Some data here"), (date, "Some data here")]

sorted(transactions, key=itemgetter(0))

for info in transactions:
    print info

I get the following, and its not sorted according to date:

(datetime.date(2010, 8, 1), 'Some data here')
(datetime.date(2010, 3, 25), 'Some data here')
(datetime.date(2010, 7, 1), 'Some data here')

How do I sort these according to date?

+3  A: 

sorted returns a sorted list, but the input itself remains unmodified. Try

transactions.sort(key=itemgetter(0))

instead. (By default tuples are compared lexicographically, so you don't need the key).

KennyTM
+2  A: 

You should use sort method if you want to modify your original data, often it is better to preserve the original data and to sort by sorted() and assign the value in new variable.

transactions.sort()

The key itemgetter is unnecessary as you want normal sorting.

My version for your whole code:

import datetime as DT

# unsorted dates
raw = ("2010-08-01","2010-03-25","2010-07-01")

datetuples = [tuple(int(numstr) for numstr in dt.split('-'))
              for dt in raw] ## numbers from strings 

transactions = [(DT.date(* dateint),
                 "Some data here") for dateint in datetuples]

transactions.sort()

for info in transactions:
    print info
Tony Veijalainen
+2  A: 

Well, the reason it's not sorted is that you haven't reassigned the sorted list back to transactions; you want:

transactions = sorted(transactions, key=itemgetter(0))

I should point out that datetime has a strptime function that does what you're doing manually:

transactions = [ ( DT.datetime.strptime( datestring, "%Y-%m-%d" ).date(), "Some data here" ) for datestring in datestrings ]

You might also prefer a collections.OrderedDict, although maybe not...

alt_transactions = collections.OrderedDict( sorted( transactions.items( ) ) )
katrielalex
Is it really that complex?? Look at KennyTM's answer.
simplyharsh
Did you even *read* the answer? The first line solves the OP's problem, and the rest is some help with the other code. Try paying attention next time.
katrielalex
+1 for the reassignment reminder. I'd want to use `date` only since I only need the date info. However, it doesn't have `strptime()`
Kit
You can use `.date()` to extract just the date.
katrielalex
Well I indeed read your answer and also the problem (with attention). Problem is solved in your first line. But list.sort is more efficient if you don't need the original list. And rest (said some help) in fact made it sound more complex than it actually is.
simplyharsh