B
B
Bur Ov2018-01-28 10:32:02
JavaScript
Bur Ov, 2018-01-28 10:32:02

How to properly sort objects by value?

Good afternoon. Is there a function that sorts an array by value? Need to get: var obj = {312312:10,3432423:2,123123:1}; Made:
var obj = {123123:1,312312:10,3432423:2};

var sortable = [];
for (var vehicle in obj) {
    sortable.push([vehicle, obj[vehicle]]);
}

sortable.sort(function(a, b) {
    return a[1] - b[1];
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Manakov, 2018-01-28
@burov0798

You wrote an invalid js construct

A
Andrey Tsvetkov, 2018-01-28
@yellow79

https://developer.mozilla.org/en/docs/Web/JavaScript...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question