简体   繁体   English

如何使用 javascript 中的两个键对对象数组进行排序

[英]How to sort an array of objects with two keys in javascript

I have an array of objects and I want to sort it based on two keys.我有一个对象数组,我想根据两个键对它进行排序。

var data = [{COMPONENT: 'PM-ABC', PRIORITY: '0.35'},
            {COMPONENT: 'PM', PRIORITY: '0.35'}
            {COMPONENT: 'PM', PRIORITY: ''}]

It should first sort on key COMPONENT (Ascending order) and then on PRIORITY ('' should come before number say '0.35')它应该首先对关键组件(升序)进行排序,然后对优先级进行排序(''应该在数字之前说'0.35')

I have tried below code which sorts based on only key ie COMPONENT我试过下面的代码,它只根据键进行排序,即 COMPONENT

data.sort(function (a, b) {
            return (a['COMPONENT'] > b['COMPONENT']) ? 1 : (a['COMPONENT'] < b['COMPONENT']) ? -1 : 0;
        });

I am expecting below result我期待以下结果

data = [{COMPONENT: 'PM', PRIORITY: ''}
        {COMPONENT: 'PM', PRIORITY: '0.35'}
        {COMPONENT: 'PM-ABC', PRIORITY: '0.35'}]

In the most basic way and without doing the sort strategy depending on some query upfront, you can just have a sort callback that first takes into account the property COMPONENT , when they are different, then property PRIORITY and only in the end the equality returning zero.以最基本的方式,无需预先根据某些查询执行排序策略,您可以只进行排序回调,首先考虑属性COMPONENT ,当它们不同时,然后是属性PRIORITY ,最后只有相等性返回零.

The point being that if COMPONENT don't differ, the first two criteria are passed and the third becomes the next property comparison.关键是,如果COMPONENT没有差异,则前两个标准通过,第三个成为下一个属性比较。

 var data = [ {COMPONENT: 'PM-ABC', PRIORITY: '0.35'}, {COMPONENT: 'PM', PRIORITY: '0.35'}, {COMPONENT: 'PM', PRIORITY: ''} ] data.sort(function(a, b) { if (a.COMPONENT < b.COMPONENT) { return -1; } if (a.COMPONENT > b.COMPONENT) { return 1; } if (a.PRIORITY < b.PRIORITY) { return -1; } if (a.PRIORITY > b.PRIORITY) { return 1; } return 0; }); console.log(data);

You can use String#localeCompare .您可以使用String#localeCompare

 let data = [{COMPONENT: 'PM-ABC', PRIORITY: '0.35'}, {COMPONENT: 'PM', PRIORITY: '0.35'}, {COMPONENT: 'PM', PRIORITY: ''}]; data.sort((a,b) => a.COMPONENT.localeCompare(b.COMPONENT) || a.PRIORITY.localeCompare(b.PRIORITY)); console.log(data);

You could sort by stages, first COMPONENT and then PRIORITY with a check.您可以按阶段排序,首先是COMPONENT ,然后是PRIORITY并勾选。

 const data = [{ COMPONENT: 'PM-ABC', PRIORITY: '0.35' }, { COMPONENT: 'PM', PRIORITY: '0.35' }, { COMPONENT: 'PM', PRIORITY: '' }]; data.sort((a, b) => a.COMPONENT.localeCompare(b.COMPONENT) || (b.PRIORITY === '') - (a.PRIORITY === '') ); console.log(data);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

Assuming that PRIORITY is expected to be a number and that an empty string '' is the same as 0 , your solution would be:假设PRIORITY应该是一个数字并且空字符串''0相同,您的解决方案是:

const data = [{ COMPONENT: 'PM-ABC', PRIORITY: '0.35' }, { COMPONENT: 'PM', PRIORITY: '0.35' }, { COMPONENT: 'PM', PRIORITY: '' }];

data.sort((a, b) => 
    a.COMPONENT.localeCompare(b.COMPONENT) ||
    ((parseFloat(a.PRIORITY) || 0) - (parseFloat(b.PRIORITY) || 0))
);

console.log(data);

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

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