Answer the question
In order to leave comments, you need to log in
How to sort data in a dictionary in ascending order by one of the keys?
I'm trying to sort the data in ascending sick order, but it's sorted incorrectly.
import json
import requests
data = requests.get("https://xn--80aesfpebagmfblc0a.xn--p1ai/covid_data.json?do=region_stats&code=RU-SAR").json()
j = json.loads(json.dumps(data, indent=4, sort_keys=True))
r = sorted(j, key=lambda x: x['sick'], reverse=False)
print(r)
Answer the question
In order to leave comments, you need to log in
Yours is a string. And sorting happens in alphabetical order.
You can sort like this:
key=lambda x: int(x['sick'])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question