簡體   English   中英

如果多個鍵具有相同的值,則按值排序 JavaScript Object 然后鍵(按字母順序)。 按順序返回排序的鍵

[英]Sort JavaScript Object by value and then key(alphabetically) if multiple keys has same value. Return sorted keys in order

優先級是按最高值排序,如果兩個或多個鍵具有相同的值,則鍵應按字母順序排序。

嘗試以下邏輯,為我工作。 尋找更好的方法。**


//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"]

優先級是按最高值排序,如果兩個或多個鍵具有相同的值,則鍵應按字母順序排序。

嘗試以下邏輯,為我工作。 尋找更好的方法。**


//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"]

優先級是按最高值排序,如果兩個或多個鍵具有相同的值,則鍵應按字母順序排序。

嘗試以下邏輯,為我工作。 尋找更好的方法。**


//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"]

優先級是按最高值排序,如果兩個或多個鍵具有相同的值,則鍵應按字母順序排序。

嘗試以下邏輯,為我工作。 尋找更好的方法。**


//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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM