-1

I have a json object of objects as below:

{
    "166":
        {
            "name":{"name":"name","val":"matt","order":""},
            "props":{"name":"props","val":"1","order":""}
        },
    "147":
        {
            "name":{"name":"name","val":"chris","order":""},
            "props":{"name":"props","val":"4","order":""}
        }
}

In JavaScript, how would I sort this object by the val property within the props object?

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
Matt Jameson
  • 582
  • 3
  • 16
  • 4
    That's not an array... – Jeff Mercado Apr 02 '15 at 14:44
  • You do not have an array in there. How would you like to sort this object, buy its properties/keys (166, 147)? Check [this answers](http://stackoverflow.com/questions/5467129/sort-javascript-object-by-key) or [this](http://stackoverflow.com/questions/1359761/sorting-a-javascript-object-by-property-name). – skobaljic Apr 02 '15 at 14:45
  • 1
    That's an object, not an array. and you DON'T deal with json like that. json is a transport encoding format. You decode that to a native structure (e.g. JS object, PHP array, blah blah blah), and you sort THAT. Then you re-encode the sorted structure into json again. – Marc B Apr 02 '15 at 14:46
  • Chill out matey, ive edited it – Matt Jameson Apr 02 '15 at 14:48
  • You have to convert this object to array first, then you can use Array's `sort` – hindmost Apr 02 '15 at 14:50
  • 3
    You can't sort it because there is no guaranteed order to an objects keys. Sorting an objects keys makes no sense. If you are relying on the order of keys in an object then what you are doing is fundamentally wrong. If you need objects to be in a certain order, then you need an array. – Matt Burland Apr 02 '15 at 14:52
  • 2
    As above, your requirement is wrong. Instead tell us what your goal is and we'll tell you how to accomplish it properly. – Lightness Races in Orbit Apr 02 '15 at 14:54

2 Answers2

1

This should get you close to what you want. It isn't pretty, but should point you in the right direction.

var json = 
{
    "166":
    {
        "name":
        {
            "name":"name",
            "val":"matt",
            "order":""
        },
        "props":
        {
            "name":"props",
            "val":"1","order":""
        }
    },
    "147":
    {
        "name":
        {
            "name":"name",
            "val":"chris",
            "order":""
        },
        "props":
        {
            "name":"props",
            "val":"4",
            "order":""
        }
    }
};

var arr = [];
for (var jsonProperty in json) {
    if (json.hasOwnProperty(jsonProperty)) {
        var propVal = json[jsonProperty]['props']['val'];
        arr.push({val: propVal, obj: json[jsonProperty]});
    }
}
var sortedArray = arr.sort(function(a,b){return a.val-b.val});
peinearydevelopment
  • 11,042
  • 5
  • 48
  • 76
1

As mentioned in numerous comments, you don't "sort" an object. Sort implies order, implies a collection, implies a data structure.

So first thing is first, you need an array of the objects. So extract them into an array by iterating over the keys. Then use Array.sort with a custom comparator.

var objectsToSort = {
    "166":
        {
            "name":{"name":"name","val":"matt","order":""},
            "props":{"name":"props","val":"1","order":""}
        },
    "147":
        {
            "name":{"name":"name","val":"chris","order":""},
            "props":{"name":"props","val":"4","order":""}
        }
}

var objects = Object.keys(objectsToSort).map(function (key) {
    return objectsToSort[key];
});

objects.sort(function (a, b) {
    return a.val - b.val;
});

I noticed that val is a string representation of a number. If you want a numeric sort, either change them to pure integers or change the sort function above to use a parseInt:

objects.sort(function (a, b) {
    return parseInt(a.val, 10) - parseInt(b.val, 10);
});

I prefer to change the actual data than the function because that parsing will run twice per comparison and sorting usually N * log(n). So it will run 2(N * log(n)).

Brandon
  • 9,822
  • 3
  • 27
  • 37