sort()方法用于类似值
我正在使用 sort()
方法对数组进行排序。当有重复值时,我想运行辅助逻辑来决定排序顺序。
例如,在下面的数组中,我想选择 red
和橙色
进行排序。
这是我的排序功能:
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
和橙色
的顺序,对数组没有影响。
[
{
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()
方法的基本误解。
阅读 docs ,返回 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
时,围绕初始功能的进一步澄清
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果
intectoryTotal
值相等,则应根据进行字符串比较返回排序结果(使用 option 值的noreferrer“>localecompare
)。如果要按字母顺序排序(“红色”之前的“橙色”),请否则使用,使用
localecompare
将返回-1、1或0取决于参考字符串是否(a)。选项
在上面的第一个示例中)发生在之前,之后或等于比较字符串。If the
inventoryTotal
values are equal, then you should return a sort result based on doing a string comparison (usinglocaleCompare
) of the twooption
values. If you want to sort alphabetically ('Orange' before 'Red'), useotherwise, use
localeCompare
will return either -1, 1, or 0 dependent on whether the reference string (a.option
in the first example above) occurs before, after or is equal to the comparison string.您的想法是正确的,也许需要一个更广泛的例子?
这是一个工作片段:
Your thinking is correct, perhaps a more extensive example is needed?
Here's a working snippet:
您的代码正常工作,并根据库存量表以降序排列阵列中的对象,而平等条件也可以正常工作。请参阅以下代码的输出:
并与此相比:
your code works correctly and arranges the objects in the array in descending order based on inventoryTotal and the equality condition also works correctly. See the output of the following code:
and compare with this:
您可以返回类似
比较的东西,哪个字符串以“较高”字母开头。
(由于功能返回0或1,因此需要 * 2-1)
You could return something like
which compares, which string starts with the "higher" letter.
(Since the functions returns 0 or 1 the * 2 - 1 is needed)