简体   繁体   中英

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 .

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.

The solution in the answer by Kevbot is elegant, but its application is limited to ES6 browsers with an implementation of date.parse() that conforms to the specific date format used by the OP.

Instead of adding a library such as moment.js just to avoid the date.parse() dependency, a tailored solution that will work in any JavaScript environment (including old browsers) can be made with just a few lines of code:

 var dates = [ {'August 17th 2016': [75]}, {'August 1st 2016': [5]}, {'August 28th 2016': [5]} ]; dates.sort(function(a, b){ var i, j; for(i in a); //get datestring a for(j in b); //get datestring b; return MMMDDYYYYtoSortableNumber(i) - MMMDDYYYYtoSortableNumber(j); }); console.log(dates); // MMMDDYYYYtoSortableNumber() converts datestrings // such as "April 5th 1969" to 19690405. // Month name to digit approach adapted from // https://gist.github.com/atk/1053863 function MMMDDYYYYtoSortableNumber(datestring) { var mdy = datestring.match(/\\w(\\w\\w)\\D+(\\d+)\\D+(\\d+)/); return mdy[3] * 10000 + ' anebarprayunulugepctovec'.search(mdy[1]) * 50 + mdy[2] * 1; } 

Please note that it might be safer to represent the datestrings as object values rather than object keys. They will then be easier to extract safely (and faster to access). Eg

{label: 'August 17th 2016', data: [75]}, 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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