按属性对对象数组进行排序(使用自定义顺序,而不是按字母顺序)

IT技术 javascript arrays sorting
2021-01-30 11:38:23

我想就这个小问题得到你的帮助。

我想根据代码而不是按字母顺序对这个数组进行排序。我用粗体指定了这一点,但最终还是被标记了,人们甚至不关心阅读问题

例如,我想有所有绿色的对象,那么所有的蓝色的人,然后将所有的红色的。最好的方法是什么?

[
    { code: "RED", value: 0},
    { code: "BLUE", value: 0},
    { code: "RED", value: 0},
    { code: "GREEN", value: 0},
    { code: "BLUE", value: 0},
    { code: "RED", value: 0},
    { code: "GREEN", value: 0},
    { code: "BLUE", value: 0}
]

是否可以使用排序功能来做到这一点在这种情况下,条件是什么?

4个回答

你可以为想要的订单拿一个对象。

var array = [{ code: "RED", value: 0 }, { code: "BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }],
    order = { GREEN: 1, BLUE: 2, RED: 3 };
    
array.sort(function (a, b) {
    return order[a.code] - order[b.code];
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

对于未知的颜色/值,您可以使用默认值

  • 0, 用于排序到顶部或
  • Infinity 排序到最后,
  • 或用于在其他组之间排序的任何其他值。

最后,您可以使用其他排序部分对特殊处理的项目进行排序,并使用逻辑 OR||链接

var array = [{ code: "YELLOW", value: 0 }, { code: "BLACK", value: 0 }, { code: "RED", value: 0 }, { code: "BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }],
    order = { GREEN: 1, BLUE: 2, RED: 3, default: Infinity };
    
array.sort(function (a, b) {
    return (order[a.code] || order.default) - (order[b.code] || order.default) || a.code.localeCompare(b.code);
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

嗯,这很有趣!我没想过使用对象来跟踪订单:)
2021-04-04 11:38:23

先设置自定义优先级

var codePriority = [ "GREEN", "BLUE", "RED" ];

现在在排序中使用相同的

arr.sort( function(a,b){ 
   if ( a.code == b.code ) return a.value - b.value;
   return codePriority.indexOf( a.code ) - codePriority.indexOf( b.code ) ; notice this line
})

演示

var arr = [
    { code: "RED", value: 0},
    { code: "BLUE", value: 0},
    { code: "RED", value: 0},
    { code: "GREEN", value: 0},
    { code: "BLUE", value: 0},
    { code: "RED", value: 0},
    { code: "GREEN", value: 0},
    { code: "BLUE", value: 0}
];
var codePriority = [ "GREEN", "BLUE", "RED" ];
arr.sort( function(a,b){ 
   if ( a.code == b.code ) return a.value - b.value;
   return codePriority.indexOf( a.code ) - codePriority.indexOf( b.code )
});
console.log( arr );

@Rajesh 哦,没想到 :)... 我暂时保持原样,它更具可读性。
2021-03-17 11:38:23
您可以删除if支票。尝试return codePriority.indexOf( a.code ) - codePriority.indexOf( b.code ) || a.value - b.value
2021-04-03 11:38:23

您可以实现架构数组并根据该架构数组内的元素索引进行排序。

const a = ['GREEN', 'BLUE', 'RED'];

const o = [{code:"RED",value:0},{code:"BLUE",value:0},{code:"RED",value:0},{code:"GREEN",value:0},{code:"BLUE",value:0},{code:"RED",value:0},{code:"GREEN",value:0},{code:"BLUE",value:0}];

const r = o.slice().sort(({ code: q }, { code: w }) => a.indexOf(q) - a.indexOf(w));

console.log(r);

我正在处理一种情况,我必须匹配字符串状态并专门按各种状态进行排序。.findIndex.includes最终成为最好的方法。

statusPriority 设置预期的顺序。一旦找到索引,就可以比较它们并返回到 .sort

let records = [
  {status: 'Record is ready to submit.', active: true, name: 'A'},
  {status: 'Record has been sent.', active: true, name: 'B'},
  {status: 'Activation is required.', active: true, name: 'C'},
  {status: 'Record submission failed.', active: true, name: 'D'},
  {status: 'Creation is pending.', active: true, name: 'E'},
]

console.log(records.map(r => r.status))

let statusPriority = ['creation', 'activation', 'sent', 'ready', 'failed'];
records.sort((a, b) => {
  let aIndex = statusPriority.findIndex(status => a.status.toLowerCase().includes(status));
  let bIndex = statusPriority.findIndex(status => b.status.toLowerCase().includes(status));
  return aIndex - bIndex;
})

console.log(records.map(r => r.status))