[Answer]-Can't sort python dictionary which is then output via json to the front end

1👍

Changed the return statement to the following:

return HttpResponse(simplejson.dumps({'results': vehicle_code_list}, sort_keys=True), mimetype="application/json")

Thought I had tried that already and no luck but this seems to be working.

0👍

The sorted method returns a list type. You can recast the list that is returned from the sorted method back to a dictionary using the following code.

List to dictionary code

dict(sorted(vehicle_code_list.iteritems(), key=operator.itemgetter(0)))

Leave a comment