[英]Sort JavaScript Object by value and then key(alphabetically) if multiple keys has same value. Return sorted keys in order
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"]
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"]
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"]
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"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.