简体   繁体   English

基于两层对对象数组进行排序

[英]sort array of objects based on two layers

I want to sort this array of objects where each object should be ascending in each unit and units should be sorted ascending based on order too:我想对这个对象数组进行排序,其中每个对象应该在每个unit中升序,并且单元也应该根据order升序排序:

 const userCards = [ { unit: 5, order: 1, }, { unit: 5, order: 3, }, { unit: 5, order: 2, }, { unit: 6, order: 2, }, { unit: 6, order: 1, }, { unit: 6, order: 3, }, { unit: 7, order: 1, }, { unit: 4, order: 2, }, { unit: 1, order: 1, }, { unit: 1, order: 2, }, { unit: 1, order: 3, }, { unit: 1, order: 4, }, { unit: 3, order: 2, } ] function compare(a, b) { if (a.unit > b.unit && a.order > b.order) return 1; if (a.unit < b.unit) return -1; return 0; } const res = userCards.sort(compare); console.log(res)

For instance the desired result would be :例如,期望的结果是:

{
   unit: 5,
   order: 1,
},

{
   unit: 5,
   order: 2,
},

{
   unit: 6,
   order: 1,
},

{
   unit: 6,
   order: 2,
},

As you see so far this is not happening!正如你所看到的,这还没有发生!

How can I fix this?我怎样才能解决这个问题?

function compare(a, b) {
  if (a.unit == b.unit) {
        return a.order - b.order;
    }

  return a.unit - b.unit;
}

Details are commented in example below详细信息在下面的示例中注释

 const userCards = [{ unit: 5, order: 1, }, { unit: 5, order: 2, }, { unit: 6, order: 2, }, { unit: 6, order: 1, }, { unit: 6, order: 3, }, { unit: 7, order: 1, }, { unit: 5, order: 3, }, { unit: 4, order: 2, }, { unit: 1, order: 1, }, { unit: 1, order: 2, }, { unit: 1, order: 3, }, { unit: 1, order: 4, }, { unit: 3, order: 2, } ]; /* Create a reference object array that stores the keys needed to be in order */ let orderKey = [{ key: 'unit' }, { key: 'order' }]; // Pass both object arrays function compare(objArray, sortOrder) { // Start with .sort() return objArray.sort((a, b) => { // Define counter and ternary value initial defaults let i = 0, result = 0; /* As long as counter is less than sortOrder total AND ternary is 0 ...Define A as previous value of current key... ...Define B as current value of current key... ...If A is less than B return -1 (A and B stay)... ...if A is greater than B return 1 (A and B swap)... ...else return 0 (A and B stay)... ...increment counter... */ while (i < sortOrder.length && result === 0) { let A = a[sortOrder[i].key], B = b[sortOrder[i].key]; result = A < B ? -1 : A > B ? 1 : 0; i++; } return result; }); } console.log(JSON.stringify(compare(userCards, orderKey)));

A一个 B a.unit > b.unit && a.order > b.order a.unit > b.unit && a.order > b.order a.unit > b.unit a. 单位 > b. 单位 value returned返回值
{unit: 5, order: 3} {单位:5,订单:3} {unit: 4, order: 1} {单位:4,订单:1} True真的 1 <-- meaning A > B 1 <-- 意思是 A > B
{unit: 5, order: 3} {单位:5,订单:3} {unit: 5, order: 1} {单位:5,订单:1} False错误的 False错误的 0 <-- meaning same value 0 <-- 表示相同的值
{unit: 4, order: 1} {单位:4,订单:1} {unit: 3, order: 2} {单位:3,订单:2} False错误的 True真的 -1 <-- meaning A > B -1 <-- 意思是 A > B
{unit: 1, order: 1} {单位:1,订单:1} {unit: 3, order: 2} {单位:3,订单:2} False错误的 True真的 0 <-- meaning same value 0 <-- 表示相同的值

As you can see on this example, your condition does not work as you want since it has return 0 2 times.正如您在此示例中看到的那样,您的条件无法按您的意愿工作,因为它已返回0 2 次。


Reading the Array#sort documentation the return of the sort function is the following阅读Array#sort文档,排序函数的返回如下

compareFunction(a, b) return value compareFunction(a, b) 返回值 sort order排序
> 0 > 0 sort b before a将 b 排在 a 之前
< 0 < 0 sort a before b在 b 之前排序 a
=== 0 === 0 keep original order of a and b保持 a 和 b 的原始顺序

So here you can just do the following :所以在这里你可以做以下事情:

  • if the unit are equals, return the dufference between the orders如果单位相等,则返回订单之间的差异
  • else return the difference between the units否则返回单位之间的差异
function compare(a, b) {
  if(a.unit === b.unit) return a.order - b.order 
  else return a.unit - b.unit
}

Or with one line using ternary或者用一条线使用三元

function compare = (a, b) => a.unit === b.unit ? a.order - b.order : a.unit - b.unit

Working example工作示例

 const userCards = [ { unit: 5, order: 1, }, { unit: 5, order: 3, }, { unit: 5, order: 2, }, { unit: 6, order: 2, }, { unit: 6, order: 1, }, { unit: 6, order: 3, }, { unit: 7, order: 1, }, { unit: 4, order: 2, }, { unit: 1, order: 1, }, { unit: 1, order: 2, }, { unit: 1, order: 3, }, { unit: 1, order: 4, }, { unit: 3, order: 2, } ] const compare = (a, b) => a.unit === b.unit ? a.order - b.order : a.unit - b.unit const res = userCards.sort(compare); console.log(JSON.stringify(res))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM