sort()方法用于类似值

发布于 2025-02-14 01:53:36 字数 2959 浏览 6 评论 0 原文

我正在使用 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 时,围绕初始功能的进一步澄清

I am sorting an array using the sort() method. When there is a duplicate value I'd like to run secondary logic to decide the sort order.

For example, in the below array, I'd like to choose how red and orange get sorted.

This is my sorting function:

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;

    }
  });

When a.inventoryTotal === b.inventoryTotal, I'm testing between returning 1 and -1 in order to influence sort order. However, this does not change the order in which red and orange are sorted and has no influence on the array.


 [
   {
     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] ]
   }
 ]

I've stripped everything away to the basic function above and I think my issue is a fundamental misunderstanding of the sort() method.

Reading the docs, returning 1 or -1 should reorder the array. However, it seems to ignore this return when the values are equal.

Where am I going wrong?

EDIT: Desired Outcome

When compared values are the same, I want to perform side calculations that ultimately return either -1 or 1. Depending on if 1 or -1 is returned, compared value order will be changed.

Return 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] ]
   }
 ]

Return -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] ]
   },
   
 ]

EDIT 2: Further clarification around the initial function when a.inventoryTotal === b.inventoryTotal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘和柠 2025-02-21 01:53:36

如果 intectoryTotal 值相等,则应根据进行字符串比较返回排序结果(使用 option 值的noreferrer“> localecompare )。如果要按字母顺序排序(“红色”之前的“橙色”),请

a.option.localeCompare(b.option)

否则使用,使用

b.option.localeCompare(a.option)

localecompare 将返回-1、1或0取决于参考字符串是否( a)。选项在上面的第一个示例中)发生在之前,之后或等于比较字符串。

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; }

If the inventoryTotal values are equal, then you should return a sort result based on doing a string comparison (using localeCompare) of the two option values. If you want to sort alphabetically ('Orange' before 'Red'), use

a.option.localeCompare(b.option)

otherwise, use

b.option.localeCompare(a.option)

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.

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; }

匿名。 2025-02-21 01:53:36

您的想法是正确的,也许需要一个更广泛的例子?
这是一个工作片段:

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)

Your thinking is correct, perhaps a more extensive example is needed?
Here's a working snippet:

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)

执笔绘流年 2025-02-21 01:53:36

您的代码正常工作,并根据库存量表以降序排列阵列中的对象,而平等条件也可以正常工作。请参阅以下代码的输出:

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)

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:

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)

and compare with this:

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)

一桥轻雨一伞开 2025-02-21 01:53:36

您可以返回类似

a.option.localeCompare(b.option) * 2 - 1;

比较的东西,哪个字符串以“较高”字母开头。

(由于功能返回0或1,因此需要 * 2-1)

You could return something like

a.option.localeCompare(b.option) * 2 - 1;

which compares, which string starts with the "higher" letter.

(Since the functions returns 0 or 1 the * 2 - 1 is needed)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文