1

I have this Json object that I created using json dumps:

  data.append({"word":word,'list':list , "count":count})

  value=json.dumps({"data":data })

 {"data": [{"count": 1, "word": "bob", "list": [1]}, {"count": 10, "word": "lola", "list": [2,7]}]}

I want to order this object according to "count", and have instead this output:

 {"data": [{"count": 1O, "word": "lola", "list": [2,7]}, {"count": 1, "word": "bob", "list": [2]}]}

Any help on how to order this type of objects? I have already tried this solution: How to get sorted list inside a dictionary with json.dumps() from Martijn Pieters, but it orders the list according to 'list' not count.

Thank you in advance

1
  • 1
    Simply sort data based on 'count' and then pass it to json.dumps(). Commented Dec 24, 2014 at 17:02

1 Answer 1

5

Replace the line

value=json.dumps({"data":data })

with

value = json.dumps({'data': sorted(data, key=lambda x: x['count'], reverse=True)})

You should get

{"data": [{"count": 10, "list": [2, 7], "word": "lola"}, {"count": 1, "list": [1], "word": "bob"}]}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.