[英]Sort() Method For Similar Values
我正在使用sort()
方法對數組進行排序。 當有重復值時,我想運行輔助邏輯來決定排序順序。
例如,在下面的數組中,我想選擇red
和orange
的排序方式。
這是我的排序功能:
inventoryTally.sort(function (a, b) {
if (a.inventoryTotal < b.inventoryTotal) {
return 1;
}
if (a.inventoryTotal > b.inventoryTotal) {
return -1;
}
if (a.inventoryTotal === b.inventoryTotal) {
//When inventory levels are the same, call secondary logic that further compares a and b based on sales volume of those 2 product variants
//I want the variant with higher sales volume to be ordered first
//My thinking is to dynamically return 1 or -1 so that the higher sales volume variant is ordered first
//return 1;
}
});
當a.inventoryTotal === b.inventoryTotal
時,我在返回 1 和 -1 之間進行測試以影響排序順序。 但是,這不會改變red
和orange
的排序順序,對數組沒有影響。
[
{
option: 'Blue',
inventoryTotal: 12312312,
variant: [ [Variant], [Variant], [Variant] ]
},
{
option: 'Green',
inventoryTotal: 1265,
variant: [ [Variant], [Variant], [Variant] ]
},
{
option: 'Red',
inventoryTotal: 3,
variant: [ [Variant], [Variant], [Variant] ]
},
{
option: 'Orange',
inventoryTotal: 3,
variant: [ [Variant], [Variant], [Variant] ]
}
]
我已經將所有內容剝離到上面的基本功能中,我認為我的問題是對sort()
方法的根本誤解。
閱讀文檔,返回1
或-1
應該重新排序數組。 但是,當值相等時,它似乎忽略了這個返回。
我哪里錯了?
編輯:期望的結果
當比較值相同時,我想執行最終返回 -1 或 1 的輔助計算。根據返回的是 1 還是 -1,比較值的順序將發生變化。
返回1:
if (a.inventoryTotal === b.inventoryTotal) {
return 1;
}
[
{
option: 'Blue',
inventoryTotal: 12312312,
variant: [ [Variant], [Variant], [Variant] ]
},
{
option: 'Green',
inventoryTotal: 1265,
variant: [ [Variant], [Variant], [Variant] ]
},
{
option: 'Red',
inventoryTotal: 3,
variant: [ [Variant], [Variant], [Variant] ]
},
{
option: 'Orange',
inventoryTotal: 3,
variant: [ [Variant], [Variant], [Variant] ]
}
]
返回-1:
if (a.inventoryTotal === b.inventoryTotal) {
return -1;
}
[
{
option: 'Blue',
inventoryTotal: 12312312,
variant: [ [Variant], [Variant], [Variant] ]
},
{
option: 'Green',
inventoryTotal: 1265,
variant: [ [Variant], [Variant], [Variant] ]
},
{
option: 'Orange',
inventoryTotal: 3,
variant: [ [Variant], [Variant], [Variant] ]
}
{
option: 'Red',
inventoryTotal: 3,
variant: [ [Variant], [Variant], [Variant] ]
},
]
編輯 2:當a.inventoryTotal === b.inventoryTotal
時,進一步澄清初始函數
如果inventoryTotal
值相等,那么您應該返回基於對兩個option
值進行字符串比較(使用localeCompare
)的排序結果。 如果要按字母順序排序(“橙色”在“紅色”之前),請使用
a.option.localeCompare(b.option)
否則,使用
b.option.localeCompare(a.option)
localeCompare
將返回 -1、1 或 0,具體取決於引用字符串(上面第一個示例中的a.option
)是出現在比較字符串之前、之后還是等於比較字符串。
const Variant = 'Variant' const inventoryTally = [ { option: 'Red', inventoryTotal: 3, variant: [ [Variant], [Variant], [Variant] ] }, { option: 'Blue', inventoryTotal: 12312312, variant: [ [Variant], [Variant], [Variant] ] }, { option: 'Orange', inventoryTotal: 3, variant: [ [Variant], [Variant], [Variant] ] }, { option: 'Green', inventoryTotal: 1265, variant: [ [Variant], [Variant], [Variant] ] } ] inventoryTally.sort((a, b) => { if (a.inventoryTotal != b.inventoryTotal) // sort descending return b.inventoryTotal - a.inventoryTotal; else // sort ascending return a.option.localeCompare(b.option); }) console.log(inventoryTally)
.as-console-wrapper { max-height: 100% !important; top: 0; }
你的想法是正確的,也許需要一個更廣泛的例子?
這是一個工作片段:
let inventoryTally = [{ option: 'Blue', inventoryTotal: 1265, other: 5 }, { option: 'Green', inventoryTotal: 1265, other: 50 }, { option: 'Red', inventoryTotal: 3, other: 5 }, { option: 'Orange', inventoryTotal: 3, other: 50 }, { option: 'Blorange', inventoryTotal: 5555, other: 0 } ] inventoryTally.sort(function(a, b) { if (a.inventoryTotal < b.inventoryTotal) { return 1; } if (a.inventoryTotal > b.inventoryTotal) { return -1; } if (a.inventoryTotal === b.inventoryTotal) { //When inventory levels are the same, call secondary logic that further compares a and b based on another property value if (a.other < b.other) return 1; if (b.other < a.other) return -1; return 0; } }); console.log(inventoryTally)
您的代碼可以正常工作,並根據inventoryTotal 以降序排列數組中的對象,並且相等條件也可以正常工作。 請參閱以下代碼的輸出:
let myArray = [ { option: 'Blue', inventoryTotal: 55, }, { option: 'Green', inventoryTotal: 2, }, { option: 'Red', inventoryTotal: 2, }, { option: 'Orange', inventoryTotal: 88, } ]; myArray.sort(function (a, b) { if (a.inventoryTotal < b.inventoryTotal) { return 1; } if (a.inventoryTotal > b.inventoryTotal) { return -1; } if (a.inventoryTotal === b.inventoryTotal) { if(a.option.length<b.option.length){ return 1; }else{ return -1; } } }); console.log(myArray)
並與此進行比較:
let myArray = [ { option: 'Blue', inventoryTotal: 55, }, { option: 'Green', inventoryTotal: 2, }, { option: 'Reddd', inventoryTotal: 2, }, { option: 'Orange', inventoryTotal: 88, } ]; myArray.sort(function (a, b) { if (a.inventoryTotal < b.inventoryTotal) { return 1; } if (a.inventoryTotal > b.inventoryTotal) { return -1; } if (a.inventoryTotal === b.inventoryTotal) { if(a.option.length<b.option.length){ return 1; }else{ return -1; } } }); console.log(myArray)
你可以返回類似的東西
a.option.localeCompare(b.option) * 2 - 1;
比較,哪個字符串以“更高”字母開頭。
(由於函數返回 0 或 1,因此需要 * 2 - 1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.