0

I have the following object in the qualification and I am not getting any indication of the key. I need to sort by value.

enter image description here

I did so:

let keysSorted = Object.values (arrCambo) .sort (function (a, b) {return arrCambo [a] -arrCambo [b]}); let sorted = keysSorted.sort ();

Generally, return is an array in which I lose the original values of the object I need!

phpricardo
  • 131
  • 4
  • 1
    I think we need a little more context do your question. What are you trying to accomplish and where are you hung up? – E McG Oct 23 '18 at 13:19
  • May be this can help you https://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value – Tech Yogesh Oct 23 '18 at 13:20
  • 1
    You cannot order the properties of an Object. If you need this, convert the data structure to an array of objects instead. – Rory McCrossan Oct 23 '18 at 13:21
  • Possible duplicate of [Sorting JavaScript Object by property value](https://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value) – Tarabass Oct 23 '18 at 13:24
  • @EMcG What you are trying to do is pick up objects and have them read alphabetically according to their value. I see things on the internet, are done with sorting (), always looking at the key. – phpricardo Oct 23 '18 at 13:32
  • You should post the expected array to sort as code, not an image. And a better/complete example of your code to see where it is failing. – Alvaro Castro Oct 23 '18 at 13:50

1 Answers1

1
var maxSpeed = {
    car: 300, 
    bike: 60, 
    motorbike: 200, 
    airplane: 1000,
    helicopter: 400, 
    rocket: 8 * 60 * 60
};

var sortable= [];

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

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

source

Tarabass
  • 3,132
  • 2
  • 17
  • 35