-2

From a bzr repository I can return timestamps from different revision commits by using

branch.repository.get_revision(revision_id).timestamp

after I get the timestamp I can use: datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S') to get the format of 2020-05-27 15:26:57.

I am going to store a number of these into a list and then from there I will need to sort them from the oldest date and time to the newest date and time. I looked at a bunch of the other questions already on here but none of the answers seemed to translate to this situation very easily.

  • have you tried calling `sorted()` on your list? – ai.jennetta Jun 18 '20 at 16:25
  • Where are you stuck? Sort the dates either before or after you convert. Where is your coding attempt? What is wrong with it? – Prune Jun 18 '20 at 16:33
  • Does this answer your question? [How do I sort a list of datetime or date objects?](https://stackoverflow.com/questions/14472795/how-do-i-sort-a-list-of-datetime-or-date-objects) – Trenton McKinney Jun 18 '20 at 19:41

2 Answers2

1

Since the date and time are already sorted from most-influential to least influential, just use the build-in sort() method for the list.

list = [timestamp_1, timestamp_2, ...]
list.sort()  # now its sorted

This works since all your dates/times have the same format and so on.

To make it faster, you can also leave out the conversion to a string and just use the unix-timestamp for sorting. You can always later on convert is to a string with your method, to make it readable.

Dorian
  • 1,439
  • 1
  • 11
  • 26
  • Do I need any identifiers on how to sort it besides reverse=True? Because when I run `x = mylist.sort(reverse=True)` and I print x I get None as my output – newtopython Jun 18 '20 at 16:29
  • 1
    `.sort()` is a `void` method, it returns nothing. It is operating on the list itself, so the original list is changed and you have to print that. Do not overwrite it. – Dorian Jun 18 '20 at 16:33
1

calling sorted() on your list should do the trick:

lst = ['2020-05-27 15:26:57','2020-06-27 15:26:57','2020-03-27 15:26:57']
sorted(lst)

Output: ['2020-03-27 15:26:57', '2020-05-27 15:26:57', '2020-06-27 15:26:57']

We can also use lst.sort() to do the trick in the following way:

lst.sort()
lst
Output: ['2020-03-27 15:26:57', '2020-05-27 15:26:57', '2020-06-27 15:26:57']
ai.jennetta
  • 1,076
  • 2
  • 10
  • 25
  • I think this may have worked! I'll do some test runs and make sure though. – newtopython Jun 18 '20 at 16:31
  • great, please approve the answer if so :) – ai.jennetta Jun 18 '20 at 16:32
  • 1
    this will sort based on characters in the strings - which will work since these are ISO formatted datetimes. However, it would be more logical to do the sorting based on timestamps (floating point numbers) and cast to string afterwards, don't you think? – FObersteiner Jun 18 '20 at 16:41
  • I actually need the newer dates first so all I had to do was reverse=True an it works like a charm – newtopython Jun 18 '20 at 16:43
  • @MrFuppes I agree that it may make more sense and make my code cleaner but I am not certain how to go about that process. That was what I originally had trouble with so I just converted it to a string and then asked how to sort it because I figured I could get a much cleaner response with more guidance on just sorting the string. – newtopython Jun 18 '20 at 16:47
  • 1
    @newtopython: you can sort a list of numbers with the same method - and apply strptime afterwards to all elements you need in "human readable" format. – FObersteiner Jun 18 '20 at 16:52