1

I have an object whose values I want to sort by descending order. Exemple:

//This
{
  c: 34,
  a: 30,
  b: 21
 }
 
 // Instead of
{
  a: 30,
  b: 21,
  c: 34
 }

I can sort it this way:

function sortObject (obj){
  return Object.entries(obj).sort((a, b) => (a[1] > b[1] ? 1 : -1))
  }

Then, I need to transform this sorted array back into an object. No matter the method, the object goes back to auto-sorting its key by alphabetical order. How to keep my new order?

Here is the method I've used:

sortedObject.reduce((acc, [key, value]) => Object.assign(acc, { [key]: value }), {});

Thanks!

DoneDeal0
  • 5,273
  • 13
  • 55
  • 114
  • have you taken a json from it? maybe your user agent does the sorting for `console.log`. – Nina Scholz Jun 10 '20 at 15:08
  • No it has nothing to do with my useragent, I have the same issue in node. – DoneDeal0 Jun 10 '20 at 15:10
  • your code is working fine, if you combine all parts to a working snippet. – Nina Scholz Jun 10 '20 at 15:13
  • You should change the sort compareFunction to `(a, b) => b[1] - a[1]`. If the values are same, then 0 should be returned from the function for consistent sorting – adiga Jun 10 '20 at 15:15
  • You are not returning anything from`sortObject` function and you are calling `sortObject` function. Change it to: `function sortObject (obj) { return Object.entries(....` and `var newObject = sortedObject(yourObjHere).reduce(.....)` – adiga Jun 10 '20 at 15:24

1 Answers1

0

You can use Object.fromEntries to put back into an object.

example ->

var o =
{
  c: 34,
  a: 30,
  b: 21
};

function sortObject (obj){
  return Object.fromEntries(
    Object.entries(obj).sort((a, b) => (a[1] - b[1])));
}

console.log(sortObject(o));
Keith
  • 22,005
  • 2
  • 27
  • 44
  • 1
    You can change the sort compareFunction to `(a, b) => b[1] - a[1]`. If the values are same, then 0 should be returned from the function for consistent sorting – adiga Jun 10 '20 at 15:14
  • @adiga Yes, good point. I just copied OP's code here. I'll update.. – Keith Jun 10 '20 at 15:15
  • Object.fromEntries doesn't work. – DoneDeal0 Jun 10 '20 at 15:17
  • @DoneDeal0 If you run the above snippet, it seems to work.. What browser / version are running. – Keith Jun 10 '20 at 15:20
  • I've just tried it in a codepen. It goes back to a natural a,b,c ordering. Also, typescript refuses to use this method in my real project. – DoneDeal0 Jun 10 '20 at 15:23
  • @DoneDeal0 It should not make any difference, have you a link to the codepen? Does the above snippet work for you.? – Keith Jun 10 '20 at 15:24
  • Yes, the snippet works fine in SO, not here: https://codepen.io/MonsieurLeland/pen/dyGGmBZ?editors=0011 – DoneDeal0 Jun 10 '20 at 15:25
  • 1
    @DoneDeal0 check your browser's console. Codepen's console seems to be giving wrong order – adiga Jun 10 '20 at 15:29
  • It works there too, it's just a bug in the way CodePen is displaying Objects. Look in the browsers console and you will see the correct output. I believe CodePen maybe designed its Object viewer before Object's had a defined order, recent ES Spec has giving objects a defined order based on insertion, old ES5 spec's never stipulated, so CodePen is just showing in key order instead. – Keith Jun 10 '20 at 15:29