- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to sort a Python date string list?
To sort a Python date string list using the sort function, you'll have to convert the dates in objects and apply the sort on them. For this you can use the key named attribute of the sort function and provide it a lambda that creates a datetime object for each date and compares them based on this date object.
from datetime import datetime my_dates = ['5-Nov-18', '25-Mar-17', '1-Nov-18', '7-Mar-17'] my_dates.sort(key=lambda date: datetime.strptime(date, "%d-%b-%y")) print(my_dates)
This will give the output −
['7-Mar-17', '25-Mar-17', '1-Nov-18', '5-Nov-18']
- How to convert a Python date string to a date object?
- Python How to sort a list of strings
- How to sort a list of strings in Python?
- Python – Sort String list by K character frequency
- How to extract date from a string in Python?
- How to sort the objects in a list in Python?
- How to sort a list in C#?
- Python program to sort a list of tuples alphabetically
- Python program to sort and reverse a given list
- How to correctly sort a string with a number inside in Python?
- How to sort the letters in a string alphabetically in Python?
- Python – Sort a List by Factor count
- Java Program to Map String list to lowercase and sort
- Program to sort all elements in a given list and merge them into a string in Python