简体   繁体   English

按属性对对象JavaScript数组进行排序

[英]Sort array of object javascript by attribute

I have an unsorted array of object having 2 revelant property: type , and date . 我有一个具有2个相关属性的未排序对象数组: typedate I need to sort this array: 我需要对这个数组进行排序:

  1. By date and 按日期和
  2. By type (for every year, deposit should go first before withdrawal ) 按类型(每年, deposit应先withdrawal ,然后withdrawal

Here is an example of input: 这是输入示例:

let i = [
    {
        date:'2017',
        type:'withdrawal',
        amount:-5
    },
    {
        date:'2016',
        type:'deposit',
        amount:12
    },
    {
        date:'2018',
        type:'deposit',
        amount:54
    },
    {
        date:'2017',
        type:'deposit',
        amount:20
    },
    {
        date:'2016',
        type:'withdrawal',
        amount:55
    },
    {
        date:'2018',
        type:'withdrawal',
        amount:54
    }
]  

The goal is to output something like this: 目标是输出如下内容:

let o = [
    {
        date:'2016',
        type:'deposit',
        amount:12
    },
    {
        date:'2016',
        type:'withdrawal',
        amount:55
    },
    {
        date:'2017',
        type:'deposit',
        amount:20
    },
    {
        date:'2017',
        type:'withdrawal',
        amount:-5
    },
    {
        date:'2018',
        type:'deposit',
        amount:54
    },
    {
        date:'2018',
        type:'withdrawal',
        amount:54
    }
]

So far I managed to sort the array by date using: 到目前为止,我设法使用以下方法按日期对数组进行排序:

o = i.sort((a,b)=>(a.date - b.date))

But I can't find a way to sort it by type 但是我找不到一种按类型对其进行排序的方法

You can use "sort" like below. 您可以使用“ sort”,如下所示。 By using "localeCompare" it will sort "type" in increasing order - which is "deposit" will come before "withdrawl" as "d" comes before "w" 通过使用“ localeCompare”,它将以升序对“类型”进行排序-“存款”将在“提款”之前,因为“ d”在“ w”之前

That is use this - 那就是用这个-

i.sort((a,b) => ((+a.date) - (+b.date) || (a.type.localeCompare(b.type))))
  • you sort first by "date" and if dates are same then it goes in OR condition (as a.date - b.date will be 0) to check "type" 您首先按“日期”进行排序,如果日期相同,则它处于OR条件(如a.date-b.date将为0)以检查“类型”

 let i = [ { date:'2017', type:'withdrawal', amount:-5 }, { date:'2016', type:'deposit', amount:12 }, { date:'2018', type:'deposit', amount:54 }, { date:'2017', type:'deposit', amount:20 }, { date:'2016', type:'withdrawal', amount:55 }, { date:'2018', type:'withdrawal', amount:54 } ] i.sort((a,b) => ((+a.date) - (+b.date) || (a.type.localeCompare(b.type)))) console.log(i) 

You can use string#localCompare to sort the array first based on date and then on type . 您可以使用string#localCompare首先根据date对数组进行排序,然后再根据type进行排序。

 let arr = [ { date:'2017', type:'withdrawal', amount:-5 }, { date:'2016', type:'deposit', amount:12 }, { date:'2018', type:'deposit', amount:54 }, { date:'2017', type:'deposit', amount:20 }, { date:'2016', type:'withdrawal', amount:55 }, { date:'2018',type:'withdrawal', amount:54 } ]; arr.sort((a,b) => a.date.localeCompare(b.date) || a.type.localeCompare(b.type)); console.log(arr); 

While probably not the most optimal solution, I concat'ed the type onto the date as a string for both "a" and "b" in the Array.prototype.sort method, and sorted the array based on that. 虽然可能不是最佳解决方案,但我在Array.prototype.sort方法中将类型作为字符串“ a”和“ b”连接到了日期,并根据该字符串对数组进行了排序。

i.sort((a,b) => {
  let aStr = `${a.date}${a.type}`;
  let bStr = `${b.date}${b.type}`;
  if(aStr > bStr) {
    return 1;
  } else if(aStr === bStr) {
    return 0;
  } else {
    return -1;
  }
});

Hope that helps! 希望有帮助!

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

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