1

I have following unsorted dict (dates are keys):

{"23-09-2014": 0, "11-10-2014": 0, "30-09-2014": 0, "26-09-2014": 0, "03-10-2014": 0, "19-10-2014": 0, "15-10-2014": 0, "22-09-2014": 0, "17-10-2014": 0, "29-09-2014": 0, "13-10-2014": 0, "16-10-2014": 0, "12-10-2014": 0, "25-09-2014": 0, "14-10-2014": 0, "08-10-2014": 0, "02-10-2014": 0, "09-10-2014": 0, "18-10-2014": 0, "24-09-2014": 0, "28-09-2014": 0, "10-10-2014": 0, "21-10-2014": 0, "20-10-2014": 0, "06-10-2014": 0, "04-10-2014": 0, "27-09-2014": 0, "05-10-2014": 0, "01-10-2014": 0, "07-10-2014": 0}

I am trying to sort it from oldest to newest. I've tried code:

mydict = OrderedDict(sorted(mydict .items(), key=lambda t: t[0], reverse=True))

to sort it, and it almost worked. It produced sorted dict, but it has ignored months:

{"01-10-2014": 0, "02-10-2014": 0, "03-10-2014": 0, "04-10-2014": 0, "05-10-2014": 0, "06-10-2014": 0, "07-10-2014": 0, "08-10-2014": 0, "09-10-2014": 0, "10-10-2014": 0, "11-10-2014": 0, "12-10-2014": 0, "13-10-2014": 0, "14-10-2014": 0, "15-10-2014": 0, "16-10-2014": 0, "17-10-2014": 0, "18-10-2014": 0, "19-10-2014": 0, "20-10-2014": 0, "21-10-2014": 0, "22-09-2014": 0, "23-09-2014": 0, "24-09-2014": 0, "25-09-2014": 0, "26-09-2014": 0, "27-09-2014": 0, "28-09-2014": 0, "29-09-2014": 0, "30-09-2014": 0}

How can I fix this?

EDIT: I need this to count objects created in django application in past X days, for each day.

event_chart = {}
date_list = [datetime.datetime.today() - datetime.timedelta(days=x) for x in range(0, 30)]
for date in date_list:
    event_chart[formats.date_format(date, "SHORT_DATE_FORMAT")] = Event.objects.filter(project=project_name, created=date).count()

event_chart = OrderedDict(sorted(event_chart.items(), key=lambda t: t[0]))
return HttpResponse(json.dumps(event_chart))
2
  • 2
    You need to convert those strings to datetime objects to make them sort correctly - have a look at the docs. Alternatively, if you can change format to 'yyyy-mm-dd' they will sort correctly as strings. Commented Oct 21, 2014 at 14:12
  • The problem is, I am already converting these dates from datetime objects. And each of them should localised. Please check my edit Commented Oct 21, 2014 at 14:20

2 Answers 2

0

You can use the datetime module to parse the strings into actual dates:

>>> from datetime import datetime
>>> sorted(mydict .items(), key=lambda t:datetime.strptime(t[0], '%d-%m-%Y'), reverse=True)
Sign up to request clarification or add additional context in comments.

2 Comments

I am using django, and each date should be localised. Please check my edit
Well, even better - there you already have date items - in order, even. Why do you destroy the order, only to re-sort in that order afterwards?
0

If you want to create a json response in the format: {"22-09-2014": 0, 23-09-2014": 0, "localized date": count_for_that_date} so that oldest dates will appear earlier in the output then you could make event_chart an OrderedDict:

event_chart = OrderedDict()
today = DT.date.today() # use DT.datetime.combine(date, DT.time()) if needed
for day in range(29, -1, -1): # last 30 days
    date = today - DT.timedelta(days=day)
    localized_date = formats.date_format(date, "SHORT_DATE_FORMAT")
    day_count = Event.objects.filter(project=name, created=date).count()
    event_chart[localized_date] = day_count
return HttpResponse(json.dumps(event_chart))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.