I have an object like this:
const data = {
val5: 1231,
val3: 1232,
val1: 1233,
val2: 1234,
val4: 1235,
};
And its has to be sorted by it keys but in specific order:
const sortOrder = ["val1", "val3", "val2"];
If the key is not present in the sortOrder
then it has to go below the keys that are present. And the non present keys order should (potentially) stay the same.
The result object should look like this:
{
val1: 1233, // present in sortOrder at position 0
val3: 1232, // present in sortOrder at position 1
val2: 1234, // present in sortOrder at position 2
val5: 1231, // NOT present in sortOrder
val4: 1235, // NOT present in sortOrder
}
I've been playing with sorting the keys only for now and can achieve some order but the non present keys are appearing at the top
const ordered = Object.keys(data).sort((a, b) => {
return sortOrder.indexOf(a) - sortOrder.indexOf(b);
});
[ 'val5', 'val4', 'val1', 'val3', 'val2' ]
As you can see val5
and val4
are the beginning and they should be at the end (after val2
)