1

just a rather quick question to ask as I have been looking everywhere but haven't seen something that has worked for me I suppose. Suppose I have a list of tuples where:

data = (a, b, DateTime, d)

I wish to sort the list by dateTime in descending order where the most recent date/time is first. In this case, DateTime is the third element in the list, and I don't seem why my program doesn't sort when I try to sort. Any suggestions on how to sort the hypothetical example above by datetime?

2
  • Is that tuple an item of your list, or a representation of the list itself? Can you show your sorting code? It's not entirely clear what you mean to do. Commented Dec 12, 2013 at 1:29
  • My apologies, it would be a list of tuples. It is also a representation of a list. Commented Dec 12, 2013 at 1:34

1 Answer 1

4
>>> from pprint import pprint
>>> import datetime

>>> l = [('a', 'b', datetime.datetime.now(), 'd') for _ in xrange(10)]
>>> pprint(l)
[('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 349796), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350127), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350132), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350134), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350137), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350140), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350143), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350145), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350148), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350151), 'd')]

>>> l_desc = sorted(l, key=lambda t: t[2], reverse=True)
>>> pprint(l_desc)
[('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350151), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350148), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350145), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350143), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350140), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350137), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350134), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350132), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350127), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 349796), 'd')]

>>> l_asc = sorted(l_desc, key=lambda t: t[2])
>>> pprint(l_asc)
[('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 349796), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350127), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350132), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350134), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350137), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350140), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350143), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350145), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350148), 'd'),
 ('a', 'b', datetime.datetime(2013, 12, 11, 17, 35, 0, 350151), 'd')]
Sign up to request clarification or add additional context in comments.

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.