0

I have a JavaScript object and i need to sort object by value and make reverse. Can you help me please.

const data = {
  a: 1,
  b: -1,
  c: "Abc",
  d: -5,
  e: "ebc",
  f: 10,
  g: 5,
  h: "abc",
  k: "абв"
};
const expected = {
  f: 10,
  g: 5,
  a: 1,
  b: -1,
  d: -5,
  h: "abc",
  c: "Abc",
  e: "ebc",
  k: "абв"
};
jocoders
  • 1,594
  • 2
  • 19
  • 54
  • Since `ES6` or greater, objects have a [well defined order on the keys](https://exploringjs.com/es6/ch_oop-besides-classes.html#_traversal-order-of-properties). However, and IMO, there is no sense for that, they were not designed for maintain an order. If you need ordering features, maybe you can use a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map). – Shidersz Aug 15 '19 at 19:00
  • What exactly is the criteria for sorting here? Numbers in descending order and strings in ascending order based on [`localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare)? – adiga Aug 15 '19 at 19:00

0 Answers0