D
D
DrLeo2016-07-30 22:50:32
JSON
DrLeo, 2016-07-30 22:50:32

How to sort JSON data in ascending order?

I have a JSON file with content

{"pics":3135,"position":{"saly1337":271,"Lodsek":546}}
In position, you need to arrange all nicknames in descending order of a numerical value, how to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Belyaev, 2016-07-30
@DrLeo

You have an object, objects in js have random access, and although most often object keys go in the order of addition, no one can guarantee this!
What can be done? - get an array of keys of this object and sort this array according to property values:

var keys = Object.getOwnPropertyNames(obj.position);
keys.sort((a, b) => obj.position[a] - obj.position[b]);

#
#algooptimize #bottize, 2016-07-30
@user004

google javascript sort

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question