-1

I want to sort Object consists of key and value by key(<-number) in JavaScript.

const wantToSort = {
    "0.2122": "hello",
    "0.6458": "world",
    "0.4323": "javascript",
    "0.5127": "computer",
    "1234": "want",
    "1090": "sort",
    "700": "this",
    "400": "object",
    "500": "key",
    "950": "number",
    "324": "stack",
    "1433": "overflow",
    "123": "developer",
    "353": "program",
};

I want to sort this object, I need result like,

const sortedResult = {
    "1433": "overflow",
    "1234": "want",
    "1090": "sort",
    "950": "number",
    "700": "this",
    "500": "key",
    "400": "object",
    "353": "program",
    "324": "stack",
    "123": "developer",
    "0.6458": "world",
    "0.5127": "computer",
    "0.4323": "javascript",
    "0.2122": "hello",
};

So when console.log(sortedResult); result is like

{
    "1433": "overflow",
    "1234": "want",
    "1090": "sort",
    "950": "number",
    "700": "this",
    "500": "key",
    "400": "object",
    "353": "program",
    "324": "stack",
    "123": "developer",
    "0.6458": "world",
    "0.5127": "computer",
    "0.4323": "javascript",
    "0.2122": "hello",
}

How can I get sorted result?

stacklsy
  • 1
  • 1
  • Note especially the highly-rated comment to the duplicate question. While it's a bit out-of-date, it turns out to be true for your case, regardless. The iteration order of objects is standardized, having mostly to do with the order of property insertion, but with exceptions for small-integer keys: very ugly. – Scott Sauyet May 20 '22 at 13:45

0 Answers0