Let's say I have a json (file) that looks like this
[
{
"id": 300,
"name": "B"
},
{
"id": 400,
"name": "C"
},
{
"id": 200,
"name": "A"
}
]
parsing out the id getting
for i in file
id = (int) (i['id'])
#Now I define a variable (difference, which is id-200)
difference = id - 200
I want to sort this JSON by the user defined variable difference, from lowest to highest. So, when sorted, it will look like:
[
{
"id": 200,
"name": "A"
},
{
"id": 300,
"name": "B"
},
{
"id": 400,
"name": "C"
}
]
How do I use the user defined difference variable as the key when sorting?
'id'?(type) valuecast is not part of Python's syntax. It just work because(int) (i['id'])will becomeint(i['id']).