1

if I have an object that looks like:

let object = {
  name1 : {
    number : .5,
    otherVar : 'something'
  }, 
  name2 : {
    number : .7,
    otherVar : 'text'
  },
  name3 : {
    number : -.1,
    otherVar : 'some words'
  }
};

how could I sort it by the number value and get an array:[ name3, name1, name2]?

0

6 Answers 6

2

Here is another es6 exampel:

let object = {
  name1 : {
    number : .5,
    otherVar : 'something'
  }, 
  name2 : {
    number : .7,
    otherVar : 'text'
  },
  name3 : {
    number : -.1,
    otherVar : 'some words'
  }
};

let k = Object.keys(object).sort(((a, b) => object[a].number > object[b].number));
console.log(k);

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

2 Comments

that's a really nice inline solution, that rules :)
My solution wasn't so awsome, that's why a liked it more, I learned now how to do it :D
2

Here's a one liner:

Object.keys(object).map(key => object[key]).sort((a, b) => a.number - b.number)

1 Comment

Niiiiiice one! must be the top ;)
2

let sorted = Object.keys(obj).map(key => obj[key]).sort((a, b) => a.number - b.number);

Explanation:

  • Object.keys(obj) will return an array of keys ['name1', 'name2'...]
  • .map will turn that array into an array of values
  • .sort takes a compare function and returns the sorted array (see documentation)

Recommendation: don't call your objects object :)

3 Comments

Okey, here is an example of a perfect answer. Thanks for sharing :)
actually this is not quite what i was looking for, i want to get an array of the keys, not he objects at the key, so instead of [{number : -.1,..} .. i want ['name3', 'name1'..
Thanks @DamianLattenero. To get just the names, you can re-map the resulting array, and get the key names.
1

Make an array that contains the object properties with the names added to them:

var array = [];
for (var key in object) {
    object[key].name = key;
    array.push(object[key]);
}

Then sort that array by the number:

array.sort((a, b) => a.number - b.number);

Then get the names out of the array:

var sortedNames = array.map(e => e.name);

Full demo:

let object = {
  name1 : {
    number : .5,
    otherVar : 'something'
  }, 
  name2 : {
    number : .7,
    otherVar : 'text'
  },
  name3 : {
    number : -.1,
    otherVar : 'some words'
  }
};

var array = [];
for (var key in object) {
    object[key].name = key;
    array.push(object[key]);
}
array.sort((a, b) => a.number - b.number);
var sortedNames = array.map(e => e.name);
console.log(sortedNames);

1 Comment

wooooow, that's awsome, clever as far... :) thanks for sharing
0

var items = [];
items = JSON.parse(object);
}); //will give you an array

function compare(a,b) {
  if (a.number < b.number)
    return -1;
  if (a.number > b.number)
    return 1;
  return 0;
}
items.sort(compare); //array will be sorted by number

2 Comments

How will JSON.parse(object) give you an array?
It is close enough, but why don't you use the keys() functions to get the array?
0

It should do the dirty work, first you flat your object into an array of objects, then you create your custom compare function, finally sort, and voila!

var keys = Object.keys(object);

function compare(a,b) {
  if (object[a].number < object[b].number)
    return -1;
  if (object[a].number > object[b].number)
    return 1;
  return 0;
}
var res = keys.sort(compare);

2 Comments

This won't work. There's no number property in the keys.
Yeaaah @Barmar good eyes! you were absolutly right! thanks a lot!

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.