if isinstance(obj, collections.Mapping):
    return collections.OrderedDict((key, sort_it(value, sort_order=
        sort_order)) for key, value in sorted(list(obj.items()), key=cmp_to_key(
        cmp_func)))
elif isinstance(obj, iterable_types):
    return type(obj)([sort_it(value, sort_order=sort_order) for value in obj])
else:
    return obj
sort_order = ['id', 'rn', 'dn', 'vendor', 'model', 'serial', 'name',
    'description', 'traypresence']
sample_data_uri = 'https://bit.ly/1jOpQF2'
print(json.dumps(sort_it(json.loads(urllib.request.urlopen(sample_data_uri).read()
    )), indent=4))
print(json.dumps(sort_it(json.loads(urllib.request.urlopen(sample_data_uri).read()
    ), sort_order=sort_order), indent=4))
python - complex sorting of nested data