So I have a JSON file that looks like this:
{
"PlayerA": {
"val": 200,
"level": 1
},
"PlayerB": {
"val": 1000,
"level": 1
},
"PlayerC": {
"val": 30,
"level": 1
}
}
And I want it to be sorted by "val," so that it looks like this:
{
"PlayerB": {
"val": 1000,
"level": 1
},
"PlayerA": {
"val": 200,
"level": 1
},
"PlayerC": {
"val": 30,
"level": 1
}
}
How would I go about doing this?
Try this:
data = {
"PlayerA": {
"val": 200,
"level": 1
},
"PlayerB": {
"val": 1000,
"level": 1
},
"PlayerC": {
"val": 30,
"level": 1
}
}
data = sorted(data.items(), key=lambda x: x[1]["val"], reverse=True)
print(data)
# [('PlayerB', {'val': 1000, 'level': 1}), ('PlayerA', {'val': 200, 'level': 1}), ('PlayerC', {'val': 30, 'level': 1})]
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.