How to sort Python list with date

python sort datetime descending
sort dataframe by date python
sorted python
datetime python
python sort datetime strings
python sort timestamps
sort datetime column python
sort list of dates java

I have a list in Python like this

myList = ['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']

i want to sort this list by the dates

Here's a method that should work in a more general case:

from dateutil.parser import parse

myList = [
    'http://google.com Google 2018-07-10',
    'http://apple.com Apple Inc 2018-07-11',
    'Foo 2017-07-13 http://whatever.com',
    'http://microsoft.com Microsoft 2018-07-12',
    '2015-07-15 http://whatever.com Whatever'
]

dct = {parse(v, fuzzy=True): v for v in myList}
print([dct[k] for k in sorted(dct, reverse=True)])
print([dct[k] for k in sorted(dct)])

This way you won't be forced to have dates at the end of the list strings, output:

['http://microsoft.com Microsoft 2018-07-12', 'http://apple.com Apple Inc 2018-07-11', 'http://google.com Google 2018-07-10', 'Foo 2017-07-13 http://whatever.com', '2015-07-15 http://whatever.com Whatever']
['2015-07-15 http://whatever.com Whatever', 'Foo 2017-07-13 http://whatever.com', 'http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']

How do I sort a list of datetime or date objects?, Given a list of dates in string format, write a Python program to sort the list of dates in ascending order. Examples: Input : dates = [“24 Jul 2017”, “25 Jul 2017”, “11  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.

Using sorted with lambda in key

Ex:

myList = ['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']

print( sorted(myList, key= lambda x: x.split()[-1], reverse=True) )
print( sorted(myList, key= lambda x: x.split()[-1]) )

Output:

['http://microsoft.com Microsoft 2018-07-12', 'http://apple.com Apple Inc 2018-07-11', 'http://google.com Google 2018-07-10']
['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']

Python: How to Sort a List? (The Right Way), Use list. sort() to sort a list of datetime objects in-place. yesterday = date. today() - timedelta(days=1) today = date. today() tomorrow = date. today() + timedelta(days=1) date_list =[today, tomorrow, yesterday] print(date_list) date_list. sort() print(date_list) In Python, we have the datetime module which makes date based comparison easier. The datetime.strptime() function is used to convert a given string into datetime object. It accepts two arguments: date (string) and format (used to specify the format. for eg: %Y is used for specifying year) and returns a datetime object.

You can split each string, take the last part, and sort by this part:

myList = [
    'http://apple.com Apple Inc 2018-07-11', 
    'http://google.com Google 2018-07-10',     
    'http://microsoft.com Microsoft 2018-07-12'
]

sorted(myList, key=lambda s: s.split()[-1])

Output:

['http://google.com Google 2018-07-10',
 'http://apple.com Apple Inc 2018-07-11',
 'http://microsoft.com Microsoft 2018-07-12']

Python, Python sort list of dates. In the next example, we sort a list of dates. sort_date.py. #!/usr/bin/env python3 from datetime import datetime values  Python List sort() The sort() method sorts the elements of a given list. The sort() method sorts the elements of a given list in a specific order - Ascending or Descending.

You can also sort the list by applying datetime.strptime() to key:

>>> from datetime import datetime
>>> myList = ['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']
>>> sorted(myList, key=lambda x: datetime.strptime(x.split()[-1], '%Y-%m-%d'))
['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']

Note: This might be over complicating it slightly, since ISO formats dates, and sorts string dates perfectly fine, as shown in the other answers. Using strptime() is to just ensure that the dates are sorted by correct date format.

How to sort a list of datetime objects in Python, Sorted datetime objects with Python. gistfile1.py. # The trick is to pass timedelta object as a sort key and not to use cmp() function. import datetime. The sort () method sorts the list ascending by default. You can also make a function to decide the sorting criteria (s). list .sort (reverse=True|False, key=myFunc) Parameter Values. Optional. reverse=True will sort the list descending. Default is reverse=False. Optional. A function to specify the sorting criteria (s) Sort the list descending:

Python sort list - sorting list elements in Python, Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted list  You have a python list and you want to sort the items it contains. Basically, you can either use sort or sorted to achieve what you want. The difference between sort and sorted is that sort is a list method that modifies the list in place whereas sorted is a built-in function that creates a new list without touching the original one.

Sorted datetime objects with Python · GitHub, If you have a little function to return those values for a given date string then you can compare the numeric values to sort them. In fact, there's no reason to return  Javascript sort array of objects in reverse chronological order. javascript,arrays,sorting. As PM 77-1 suggests, consider using the built–in Array.prototype.sort with Date objects. Presumably you want to sort them on one of start or end: jobs.sort(function(a, b) { return new Date(a.ys, a.ms-1) - new Date(b.ys, b.ms-1); })

Sorting HOW TO, This easily sorts the list by datetime which was cool but I wasn't sure what the whole key and lambda thing was all about. Key Parameter. Referencing the How To  Python list sort() The sort function can be used to sort a list in ascending, descending or user defined order. To sort the list in ascending order. This will sort the given list in ascending order. This function can be used to sort list of integers, floating point number, string and others.

Comments
  • Have you tried something?
  • yes, i've tried to sort dictionary before appending but my dictionary's key values are always changing so i couldn't figure it out with list
  • thank you! but could you please explain me how python handle random list data in to dictionary like this?