1

I'm trying to sort this by time and I have no clue where to start.

[
    A(id=u'BLI52E', time=datetime.datetime(2014, 11, 30, 9, 52, 49, 209102)),
    A(id=u'DHCS4J', time=datetime.datetime(2014, 11, 30, 9, 53, 36, 495319)),
    A(id=u'4HMS4J', time=datetime.datetime(2014, 11, 30, 9, 53, 16, 395329)),
    A(id=u'GFTS4J', time=datetime.datetime(2014, 11, 30, 9, 53, 36, 295319)),
    A(id=u'BH244K', time=datetime.datetime(2014, 11, 30, 9, 53, 23, 591319)),
]
JackDev
  • 4,891
  • 1
  • 39
  • 48
mrmo123
  • 725
  • 1
  • 8
  • 23
  • 2
    It looks like a list (of `A` objects) to me. Isn't it? – falsetru Nov 30 '14 at 10:19
  • 1
    You need more info. What information do you want to sort on? Then just past a comparison function to the list.sort() method. Also I agree with falsetru it's exactly what I imagine a list of A's to look like. – demented hedgehog Nov 30 '14 at 10:20
  • Thanks for the responses, I'm trying to sort by time – mrmo123 Nov 30 '14 at 10:25
  • Thanks falsetru and demented hedgehog for leading me to the solution :) http://stackoverflow.com/questions/5055812/sort-python-list-of-objects-by-date – mrmo123 Nov 30 '14 at 10:35
  • Please mark the correct answer bellow and if you wanted in reverse you should mention that in the question... no need to edit questions with answers.. – Lipis Nov 30 '14 at 12:10
  • I believe this is a list of namedtuples, https://docs.python.org/2/library/collections.html#collections.namedtuple – JackDev Nov 30 '14 at 14:02
  • Please include the definition of `A`. – wwii Nov 30 '14 at 15:14
  • Does this answer your question? [Pythonic way to sorting list of namedtuples by field name](https://stackoverflow.com/questions/12087905/pythonic-way-to-sorting-list-of-namedtuples-by-field-name) – Georgy Mar 22 '20 at 16:10

1 Answers1

3

When sorting on "keys" of named tuples contained within a list, you could do the following:

spam = [
            A(id=u'BLI52E', time=datetime.datetime(2014, 11, 30, 9, 52, 49, 209102)),
            A(id=u'DHCS4J', time=datetime.datetime(2014, 11, 30, 9, 53, 36, 495319))
       ]
spam.sort(key=lambda x:x.time)

Extracted from here: Python: Tuples/dictionaries as keys, select, sort

Community
  • 1
  • 1
JackDev
  • 4,891
  • 1
  • 39
  • 48
  • 1
    Thanks happyhamburger for taking the time to answer my question. I found this to be the best solution `list.sort(key=lambda r: r.time, reverse=True)` – mrmo123 Nov 30 '14 at 10:45
  • Hehe, yeah had to change my answer there ;) – JackDev Nov 30 '14 at 10:47
  • could you just remove the "regular tuple sorting" because this isn't a tuple `tuples use parentheses and lists use square brackets.` – mrmo123 Nov 30 '14 at 10:49
  • Fair point, I guess what I meant to show was the difference in sorting a list of standard tuples, as opposed to sorting a list of named tuples. In other words using a traditional tuple, you could order them by specifying the index to order on (since they have no key). However, since you are using named tuples, the treatment is slightly different. – JackDev Nov 30 '14 at 13:55