Answers for "sort json object by key value javascript"

1

sort json object by key value javascript

var json = [{
    "name": "user1",
    "id": 3
}, {
    "name": "user2",
    "id": 6
}, {
    "name": "user3",
    "id": 1
}];

// Asc. order
json.sort(function(a, b){
    return a.id - b.id;
});

// desc order
json.sort(function(a, b){
    return b.id - a.id;
});
Posted by: Guest on March-17-2021

Code answers related to "sort json object by key value javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language