I have below list:
[
{
"attributes":
[
{
"label": "Event Date",
"value": "Tue, 29 Jun 2021 18:30:00 GMT"
}
],
"event": "DT2"
},
{
"attributes":
[
{
"label": "DT3",
"value": true
},
{
"label": "Event Date",
"value": "Thu, 22 Jul 2021 18:30:00 GMT"
}
],
"event": "DT5"
},
{
"attributes": [
{
"label": "DT6",
"value": 1.0
},
{
"label": "Event Date",
"value": "Thu, 08 Jul 2021 18:30:00 GMT"
}
],
"event": "DT7"
}
]
Above data is a list
of dict
. Each dict
has a list
of attributes
. attriutes
is again a dict which has many items and Event Date
. I have to sort all the dicts inside list based on ascending order of Event Date
. How can we achieve this?
You need to get the datetime string firstly, then format the datetime string:
import datetime
lst = [
{
"attributes":
[
{
"label": "Event Date",
"value": "Tue, 29 Jun 2021 18:30:00 GMT"
}
],
"event": "DT2"
},
{
"attributes":
[
{
"label": "DT3",
"value": True
},
{
"label": "Event Date",
"value": "Thu, 22 Jul 2021 18:30:00 GMT"
}
],
"event": "DT5"
},
{
"attributes": [
{
"label": "DT6",
"value": 1.0
},
{
"label": "Event Date",
"value": "Thu, 08 Jul 2021 18:30:00 GMT"
}
],
"event": "DT7"
}
]
lst.sort(key=lambda x: datetime.datetime.strptime(next(attr["value"] for attr in x["attributes"] if attr["label"] == "Event Date"), "%a, %d %b %Y %X %Z"))
Or more readable way:
def check(x):
for attr in x["attributes"]:
if attr["label"] == "Event Date":
return datetime.datetime.strptime(attr["value"], "%a, %d %b %Y %X %Z")
lst.sort(key=check)
And gave me:
[{'attributes': [{'label': 'Event Date',
'value': 'Tue, 29 Jun 2021 18:30:00 GMT'}],
'event': 'DT2'},
{'attributes': [{'label': 'DT6', 'value': 1.0},
{'label': 'Event Date', 'value': 'Thu, 08 Jul 2021 18:30:00 GMT'}],
'event': 'DT7'},
{'attributes': [{'label': 'DT3', 'value': True},
{'label': 'Event Date', 'value': 'Thu, 22 Jul 2021 18:30:00 GMT'}],
'event': 'DT5'}]
You want to sort by key.
This is how you would do it for a simpler example:
my_list = [
{
"foo": 5,
"bar": 4,
},
{
"foo": 3,
"bar": 2,
},
{
"foo": 1,
"bar": 0,
},
]
# This will sort the list according to the "foo" in each dictionary.
my_list.sort(key=lambda dictionary: dictionary["foo"])
For your specific case, since it is more complicated, I would create a function instead of using a lambda expression. Additionally, I will use datetime
to help me compare the date strings.
from datetime import datetime
def key_function(dictionary):
attributes = dictionary["attributes"]
for attribute in attributes:
if attribute["label"] == "Event Date":
date_string = attribute["value"]
date = datetime.strptime(date_string, "%a, %d %b %Y %X %Z")
# Compare dates according to their POSIX timestamp
# I am assuming the dates will be after 1970
return date.timestamp()
raise ValueError("Event Date attribute wasn't found")
my_list.sort(key=key_function)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.