How to order a JSON in descending order?

Asked

Viewed 4,861 times

4

I have a JSON value that I want to order it in descending order (I think in the example will give to understand).

Example:

var json = {
    'um': {
        'cont': '5'
    },
    'dois': {
        'cont': '10'
    }
    ...
};

So in this case, it should be ordered by the 'cont' value of each son of json.

In the example I used fixed values, but in the code I will use will have many more children in json and the cont values will be different too.

I think I understand, but in the end the figure should be: ['two', 'one'];

  • 3

    Two remarks to guide future visitors to this question: 1) this is not JSON, it is a literal Javascript object (JSON would serialize that as a string); 2) objects in js are not ordered, you need an array, as indicated in Miguel Angelo’s answer.

2 answers

4


1

I’ve used that one library for sorting and also for filters, in javascript arrays (JSON).

I just don’t know if in your case you can change your object to an array, so it can be ordered.

If you can use an array, this is a great approach.

That library Jlinq is very good, I never had problems with her.

It works with several hierarchical levels of the JSON object, and already implements the comparer proposed by @Miguel.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.