S
S
schokk64rus2020-05-03 17:08:54
Python
schokk64rus, 2020-05-03 17:08:54

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

1 answer(s)
S
Sergey Pankov, 2020-05-03
@schokk64rus

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 question

Ask a Question

731 491 924 answers to any question