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

时间:2017-11-07 13:05:54

标签: javascript arrays sorting

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

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

例如,我希望拥有所有绿色对象,然后是所有蓝色对象,然后是所有红色对象。最好的方法是什么?

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

是否可以使用 sort 功能执行此操作?那种情况会是什么情况?

4 个答案:

答案 0 :(得分:7)

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

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排序到最后,
  • 或在其他组之间进行排序的任何其他值。

最后,您可以使用logical 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; }

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

首先设置自定义优先级

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

答案 3 :(得分:0)

我正在处理一种情况,我必须匹配一个字符串状态并按各种状态专门排序。 .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))