Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python sort date
#9
(Nov-04-2020, 05:23 PM)deanhystad Wrote: I get a syntax error working with you code. Is there a paste error in you post? Why does the date time pattern appear twice?


No, I tested it again. Does work with Python 3.9 and should work with older versions.
Maybe the comments are confusing the repl, if you use copy&paste.

The parse_date was made twice to show:
  1. How to split tasks into smaller easier tasks -> better for testing. For example, you can use this function to test each line. No file for testing needed at all.
  2. How to catch Exceptions, handle them, retuning a default value for sorting.

To understand the sorting part, try this:

sort(sorted([1, 2, 3, 4, "a"]))
Error:
TypeError Traceback (most recent call last) <ipython-input-4-f654efc1df6a> in <module> ----> 1 sorted([1, 2, 3, 4, "a"]) TypeError: '<' not supported between instances of 'str' and 'int'
Same with datetime objects.
This works because datetime is comparable with datetime:
sorted([datetime.min, datetime.max, datetime(2020,1,1), datetime(1990,1,1)])
But this won't work:
sorted([datetime(2020,1,1), 1])
Error:
TypeError: '<' not supported between instances of 'int' and 'datetime.datetime'
This is why a key-function for sorting should always return the same type.
You could remove lines with wrong formatting before you sort.
Or you have a situation, where you want still to keep the lines with wrong format and sorting them.

There is no one universal solution for all.
Keep learning the basics before you switch to pandas.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
python sort date - by beginner2020 - Nov-04-2020, 04:51 PM
RE: python sort date - by buran - Nov-04-2020, 05:16 PM
RE: python sort date - by DeaD_EyE - Nov-04-2020, 05:18 PM
RE: python sort date - by beginner2020 - Nov-04-2020, 05:52 PM
RE: python sort date - by DeaD_EyE - Nov-04-2020, 06:02 PM
RE: python sort date - by beginner2020 - Nov-04-2020, 06:28 PM
RE: python sort date - by DeaD_EyE - Nov-05-2020, 01:14 PM
RE: python sort date - by deanhystad - Nov-04-2020, 05:23 PM
RE: python sort date - by DeaD_EyE - Nov-04-2020, 05:54 PM
RE: python sort date - by chrischarley - Nov-04-2020, 05:23 PM
RE: python sort date - by deanhystad - Nov-04-2020, 05:30 PM
RE: python sort date - by chrischarley - Nov-04-2020, 05:52 PM
RE: python sort date - by buran - Nov-04-2020, 06:33 PM
RE: python sort date - by beginner2020 - Nov-06-2020, 03:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 363 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 778 Jan-20-2024, 04:45 AM
Last Post: 1418
  How to see the date of installation of python modules. newbieAuggie2019 4 1,773 Mar-31-2023, 12:40 PM
Last Post: newbieAuggie2019
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,390 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Date format and past date check function Turtle 5 4,494 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to sort values descending from a row in a dataframe using python sankarachari 1 1,461 Aug-16-2021, 08:55 AM
Last Post: jamesaarr
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,302 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  How to add date and years(integer) to get a date NG0824 4 2,972 Sep-03-2020, 02:25 PM
Last Post: NG0824
  What's the best way to sort this data with Python? xtrax 2 1,744 Mar-15-2020, 08:08 AM
Last Post: Larz60+
  Date from from excel to Python. atp_13 1 1,815 Nov-24-2019, 12:26 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020