Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort array of objects where keys are dates

I have searched for this question and no existing answer seems to apply. Consider the following:

[
  { 'August 17th 2016': [75] }, // 75 is the length of the array which contains up to 75 objects ... 
  { 'August 1st 2016': [5] },
  { 'August 28th 2016': [5] },
  ...
]

What is the best way to sort the objects in this array by their date and still keep the "english" representation of their key?

Note: The key is used as a chart label.

Everywhere I look array.sort is used, but that's on the object's key of say created_at.

The result should be:

[
  { 'August 1st 2016': [5] },
  { 'August 17th 2016': [75] }
  { 'August 28th 2016': [5] },
  ...
]

I am not sure how to proceed so I don't have anything to show.

like image 221
TheWebs Avatar asked Dec 11 '16 19:12

TheWebs


People also ask

How to sort an array of objects by date in JavaScript?

To sort an array of object by date, there is a number of methods but we’re going to see few of the most preferred methods. Date Object: The Date object in JavaScript is used to represent a moment of time. This time value is since 1 January 1970 UTC (Coordinated Universal Time). We can create date using the Date object by calling the new Date () ...

How to sort ArrayList and sortitem in Java?

Now in the main method use Collections.sort () method and pass the ArrayList and ‘ SortItem‘ class object to it, it will sort the dates, and output will be generated. Note : This code will sort the dates in Ascending order. If you want to change the order of sorting you can refer to the program below:

How to sort array of JSON objects with date key in angular?

How to Sort Array of JSON objects with date key in Angular? Let’s create an angular application and create a new component array-json-dates.component. Array Sort method sort the arrays in ascending or descending order. Here is html component.

How to sort an array by date in Python?

array.sort (function (a,b) { // Turn your strings into dates, and then subtract them // to get a value that is either negative, positive, or zero. return new Date (b.date) - new Date (a.date); }); array.sort (function (o1,o2) { if (sort_o1_before_o2) return -1; else if (sort_o1_after_o2) return 1; else return 0; });


1 Answers

This can be accomplished by using date.parse on the object key. I took the first object key as it appears there is only 1 in each entry of the array. The tricky part is that date.parse does not work on "12th" or "1st", so, we have to temporarily replace the "th", or "st" with a ,. This way, date.parse works on the string.

var dates = [{
  'August 17th 2016': [75]
}, {
  'August 1st 2016': [5]
}, {
  'August 28th 2016': [5]
}]

const replaceOrdinals = o => {
  return Object.keys(o)[0].replace(/\w{2}( \d+$)/, ',$1');
}

dates = dates.sort((a, b) => {
  return Date.parse(replaceOrdinals(a)) - Date.parse(replaceOrdinals(b))
});

console.log(dates);

Keep in mind:

From @adeneo in the comments: Date.parse is implentation dependant. You will probably want to read through it's documentation to determine if things like time zones will mess things up. As a more sure method, you can use something like moment.js for date parsing.

like image 168
KevBot Avatar answered Nov 01 '22 13:11

KevBot