0

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?

6
  • You want to sort by 'id'? Commented May 15, 2017 at 0:36
  • No, I want to sort by the difference variable, from lowest to highest. Difference is user defined in the code, but unlike 'id' it doesn't appear in the json Commented May 15, 2017 at 0:37
  • But if you are subtracting a fixed offset, then the difference will give the same sort order. No? Commented May 15, 2017 at 0:38
  • Yeah that's true, but the important part is that I need to know how to sort by a user defined variable. Suppose instead of subtracting you're multiplying by -1 Commented May 15, 2017 at 0:41
  • 1
    Just a side note: the (type) value cast is not part of Python's syntax. It just work because (int) (i['id']) will become int(i['id']). Commented May 15, 2017 at 0:51

2 Answers 2

1

To sort by a key in a list of dicts you can do something like:

Code:

sorted(data, key=lambda x: x['id'])

Test Code:

data = [
    {"id": 300, "name": "B"},
    {"id": 400, "name": "C"},
    {"id": 200, "name": "A"}
]

print(sorted(data, key=lambda x: x['id']))

Results:

[{'id': 200, 'name': 'A'}, {'id': 300, 'name': 'B'}, {'id': 400, 'name': 'C'}]

To sort via an arbitrary function:

Any function of one variable can be passed to sort like:

def user_sort_order(record):
    return record['id'] * -1

print(sorted(data, key=user_sort_order))

Results:

[{'id': 400, 'name': 'C'}, {'id': 300, 'name': 'B'}, {'id': 200, 'name': 'A'}]
Sign up to request clarification or add additional context in comments.

2 Comments

This is not what I'm aiming for; I don't want to sort by ID, i want to sort using the difference variable as the key
The result is the same regardless if you sort by ID or by the difference variable but how do you sort this using the difference variable as the key?
0

Not 100% sure what the difference variable is supposed to do, but you can sort it like this:

sorted(data, key=lambda x: x['id'] - 200)

Perhaps surround it with a abs is the difference can be a negative number:

sorted(data, key=lambda x: abs(x['id'] - 200))

Btw, unless you have IDs that are less than 200, and the "difference" can be negative, will this be the same as sorting it by the ID numeric value (if I've understood what you want correctly).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.