I have the below code that sorts object in an alphabetic order, I want to get away from having an alphabetic order and sort on my own chosen order.
var obj = {
c: "see",
a: "ay",
b: "bee",
d: "dee"
};
console.log("Object order:");
var key, keys = [];
for (key in obj) {
keys.push(key);
}
console.log(keys.map(function(key) { return key + ": " + obj[key];}).join(", "));
console.log("Sorted order:");
keys = Object.keys(obj).sort()
console.log(keys.map(function(key) { return key + ": " + obj[key];}).join(", "));
The above will output the object order of a, b, c, d - but lets say I want an array to have my own order e.g.
var sortedArray = ['b','c','d','a'];
So how do I know implement this sortedArray into my .sort?
Expected outcome:
var newObj = {
b: "bee",
c: "cee",
d: "dee",
a: "ay"
};
Thanks