简体   繁体   English

按月对对象数组进行排序

[英]Sort an array of object on month basis

I am working with the dashboard and I have a chart in which I have to show count against month label.我正在使用仪表板,我有一个图表,我必须在其中显示针对月份标签的计数。 I have an array of objects with count and month Key.我有一组带有计数和月份键的对象。 I want to sort this array of objects in Ascending order with month key value.我想用月份键值按升序对这个对象数组进行排序。 I tried below code works fine in chrome but array does on get sorted in Firefox.我试过下面的代码在 chrome 中工作正常,但数组在 Firefox 中被排序。

var xAxis =  [
{count: 0, month: "Jan 2019"},
{count: 7, month: "Dec 2019"},
{count: 0, month: "Feb 2019"},
{count: 0, month: "Jul 2019"}
] 

 xAxis.sort(function(a, b) {
  var c = new Date(a.month);
  console.log("TCL: CountByUserGraph -> makeApiCall -> c", c)
  // Chrome showing  ==> Tue Jan 01 2019 00:00:00 GMT+0530 
  //firefox showing  ==> Invalid Date
  var d = new Date(b.month);
  return c - d;
});

You can create an array which contains all of the short names of the months.您可以创建一个包含所有月份短名称的数组。 Then, splitting the string in your input to get the short name of the month, and comparing it with the items in the array.然后,拆分输入中的字符串以获取月份的短名称,并将其与数组中的项目进行比较。

 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']; var xAxis = [ {count: 0, month: "Jan 2019"}, {count: 7, month: "Dec 2019"}, {count: 0, month: "Feb 2019"}, {count: 0, month: "Jul 2019"} ]; var output = xAxis.sort(function (a, b) { return months.indexOf(a.month.substr(0, 3)) - months.indexOf(b.month.substr(0, 3)) }); console.log(output);

Firefox's Date implementation cannot parse strings in MMM YYYY format like the ones in your example. Firefox 的Date实现无法像您的示例中那样解析MMM YYYY格式的字符串。 Consider using a different format.考虑使用不同的格式。 If you must use this format, consider using a library that allows you to parse these dates across browsers like Momentjs or date-fns .如果您必须使用这种格式,请考虑使用允许您跨浏览器(如Momentjsdate- fns )解析这些日期的库。

You made a typo.你打错字了。 Let me show you:让我演示给你看:

 var xAxis = [ {count: 0, month: "Jan 2019"}, {count: 7, month: "Dec 2019"}, {count: 0, month: "Feb 2019"}, {count: 0, month: "Jul 2019"} ] xAxis.sort(function(a, b) { var c = new Date(a.month); // console.log("TCL: CountByUserGraph -> makeApiCall -> c", c) // Chrome showing ==> Tue Jan 01 2019 00:00:00 GMT+0530 //firefox showing ==> Invalid Date var d = new Date(b.month); return c - d; }); console.log(xAxis);

the typo line was var d = ... .错字行是var d = ... You were setting both dates to the a value您将两个日期都设置为a

[edit] please note: the code I posted above works in Chrome, but a user Alexander posted below that the same code will not work in every browser. [编辑] 请注意:我上面发布的代码适用于 Chrome,但下面的用户 Alexander 发布的相同代码不适用于每个浏览器。

const sortByMonth = arr => {
  const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  const getIndexOfMonth = month => months.indexOf(month)
  return [ ...arr ].sort((left, right) => {
    const [ leftMonth, leftYear ] = left.month.split(' ')
    const [ rightMonth, rightYear ] = right.month.split(' ')
    if (leftYear !== rightYear) {
      return parseInt(leftYear) - parseInt(rightYear)
    }
    return getIndexOfMonth(leftMonth) - getIndexOfMonth(rightMonth)
  })
}

var xAxis =  [
  {count: 0, month: "Jan 2019"},
  {count: 7, month: "Dec 2019"},
  {count: 0, month: "Dec 2017"},
  {count: 0, month: "Jun 2018"},
  {count: 0, month: "Feb 2019"},
  {count: 0, month: "Jul 2019"},
];

sortByMonth(xAxis)
/* 
[
  { count: 0, month: 'Dec 2017' },
  { count: 0, month: 'Jun 2018' },
  { count: 0, month: 'Jan 2019' },
  { count: 0, month: 'Feb 2019' },
  { count: 0, month: 'Jul 2019' },
  { count: 7, month: 'Dec 2019' }
]
*/

This code will also compares the year as well as month此代码还将比较年份和月份

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

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