-1

How to sort an object by a given value of its child keys ?

For instance, this object:

{
    "a": {
        "name": "Edward",
        "value": 21
    },
    "b": {
        "name": "Sharpe",
        "value": 37
    },
    "c": {
        "name": "And",
        "value": 45
    },
    "d": {
        "name": "The",
        "value": -12
    },
    "e": {
        "name": "Magnetic",
        "value": 13
    },
    "f": {
        "name": "Zeros",
        "value": 37
    }
}

would become sorted by the "value" key of its child objects:

{
    "d": {
        "name": "The",
        "value": -12
    },
    "e": {
        "name": "Magnetic",
        "value": 13
    },
    "a": {
        "name": "Edward",
        "value": 21
    },
    "b": {
        "name": "Sharpe",
        "value": 37
    },
    "f": {
        "name": "Zeros",
        "value": 37
    },
    "c": {
        "name": "And",
        "value": 45
    }
}
DevonDahon
  • 7,460
  • 6
  • 69
  • 114
  • have you attempted to write code? – Bravo Aug 11 '21 at 09:39
  • @Bravo yes, I will add it to this post. – DevonDahon Aug 11 '21 at 09:40
  • @DevonDahon - did you forget to add it to the post – Bravo Aug 11 '21 at 09:48
  • 1
    You can't really sort an objects properties - more reading here https://stackoverflow.com/questions/1069666/sorting-object-property-by-values you'd be better converting to an array of objects, and then sorting them – andy mccullough Aug 11 '21 at 09:49
  • Sorry for the flag I misunderstood your question. But where is the point to order properties of an object? There is no use and is not possible... – PRSHL Aug 11 '21 at 09:51
  • I take it, @DevonDahon you haven't tried at all - answer given, with caveat – Bravo Aug 11 '21 at 09:53
  • @Bravo What I actually tried (and which works as well) is to add the key of each child object to itself, then convert the parent object to a `foo` array, sort it with `foo.sort((a, b) => a.value - b.value)` and recreate the object. – DevonDahon Aug 11 '21 at 10:14
  • yeah, don't know what you mean - anyway, answer posted, don't rely on it – Bravo Aug 11 '21 at 10:17
  • Does this answer your question? [Sorting an array of objects by property values](https://stackoverflow.com/questions/979256/sorting-an-array-of-objects-by-property-values) – Mickael B. Aug 11 '21 at 11:39
  • Thanks @MickaelB. but the post you mentioned concerns an array, not an object – DevonDahon Aug 11 '21 at 15:17
  • @DevonDahon object can not be sorted... – Alberto Sinigaglia Aug 11 '21 at 22:46

1 Answers1

1

This works

var obj = {
    "a": {
        "name": "Edward",
        "value": 21
    },
    "b": {
        "name": "Sharpe",
        "value": 37
    },
    "c": {
        "name": "And",
        "value": 45
    },
    "d": {
        "name": "The",
        "value": -12
    },
    "e": {
        "name": "Magnetic",
        "value": 13
    },
    "f": {
        "name": "Zeros",
        "value": 37
    }
};

const result = Object.fromEntries(Object.entries(obj).sort(([,{value:a}],[,{value:b}])=>a-b));
console.log(result);
console.log(Object.keys(result).join());

but while this seems to work, object property order is not strictly defined, so whose to say it will always work

for example, if some of the top level keys are numeric, this is the result

var obj = {
    "a": {
        "name": "Edward",
        "value": 21
    },
    "b": {
        "name": "Sharpe",
        "value": 37
    },
    "1": {
        "name": "And",
        "value": 45
    },
    "d": {
        "name": "The",
        "value": -12
    },
    "2": {
        "name": "Magnetic",
        "value": 13
    },
    "f": {
        "name": "Zeros",
        "value": 37
    }
};

const result = Object.fromEntries(Object.entries(obj).sort(([,{value:a}],[,{value:b}])=>a-b));
console.log(result);
console.log(Object.keys(result).join()); // want d2abf1 - get 12dabf

this is because numeric keys ALWAYS precede non-numeric, regardless of creation order - thats one thing that IS defined about object property order

Bravo
  • 6,022
  • 1
  • 10
  • 15