priority is to order by highest value, if two or more keys has same value then keys should be sorted alphabetically.
Tried below logic, worked for me. Looking for better approach.**
//Input Object:
let myObj = {
hello : 1, // key is Alpha, value is Number
zello : 5,
pillow : 6,
there : 6,
here : 6,
peppa : 2,
boww : 5
};
let flag = true, initialCount=0, finalCount=0, newArr = [];
const keysSorted = Object.keys(myObj).sort((a,b) => myObj[b]-myObj[a]);
for(let i=0; i<keysSorted.length; i++) {
if(myObj[keysSorted[i]] === myObj[keysSorted[i+1]]) {
if(flag) {
initialCount = i;
flag = false;
}
} else {
if(!flag) {
finalCount = i;
}
if(flag) {
newArr.push(keysSorted[i]);
} else {
let tempArr = keysSorted.slice(initialCount, finalCount+1);
tempArr.sort();
newArr.splice(initialCount, 0, ...tempArr);
}
flag = true;
initialCount = 0;
finalCount = 0;
}
}
console.log(newArr);
//Output:
//["here", "pillow", "there", "boww", "zello", "peppa", "hello"]