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!

6
  • 1
    I think we need a little more context do your question. What are you trying to accomplish and where are you hung up? Commented Oct 23, 2018 at 13:19
  • May be this can help you stackoverflow.com/questions/1069666/… Commented Oct 23, 2018 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. Commented Oct 23, 2018 at 13:21
  • Possible duplicate of Sorting JavaScript Object by property value Commented Oct 23, 2018 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. Commented Oct 23, 2018 at 13:32

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.