Sorting an object by value w/ `sort` and `reduce` Given an object with unsorted numeric values, such as:
const unsorted = {
  green: 80,
  blue: 90,
  red: 30,
  yellow: 100,
}

We want a data structure with the same key/value pairs sorted in descending order of the values, e.g.:
const sorted = {
  yellow: 100,
  blue: 90,
  green: 80,
  red: 30,
}

My solution:
function sortObjectByValue (obj) {
  const sorted = Object.keys(obj)
    .sort((a, b) =&gt; obj[b] - obj[a])
    .reduce((acc, cur) =&gt; {
      acc[cur] = obj[cur]
      return acc
    }, {})

  return new Map(Object.entries(sorted))
}


What I hope could be improved:

Two iterations, first sort then reduce; this seems inefficient.
Using Map. I wish we could just use objects, it would make using this elsewhere much easier. I doubt this can be avoided though, as we need to ensure that the order is preserved.
Here's my proposal:
function sortObjectByValue (obj) {
  const map = new Map()

  Object.entries(obj)
    .sort((a, b) =&gt; obj[b][1] - obj[a][1])
    .forEach(([key, value]) =&gt; {
      map.set(key, value)
    });

  return map;
}

You don't need to create a sorted object unless you want to use it, I understand that you are using a Map so that you can preserve the order of the elements, but keep in mind this:

Note: Since ECMAScript 2015, objects do preserve creation order for
string and Symbol keys. In JavaScript engines that comply with the
ECMAScript 2015 spec, iterating over an object with only string keys
will yield the keys in order of insertion.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
So with that in mind we can skip the Map and we have something like this:
function sortObjectByValue2 (obj) {
  return Object.keys(obj)
    .sort((a, b) =&gt; obj[b] - obj[a])
    .reduce((acc, cur) =&gt; {
      acc[cur] = obj[cur]
      return acc
    }, {})
}