简体   繁体   English

如何对键是日期的对象数组进行排序

[英]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 . 我到处都看array.sort使用,但是这是在说的对象的关键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. 这可以通过在对象键上使用date.parse来完成。 I took the first object key as it appears there is only 1 in each entry of the array. 我拿了第一个对象键,因为它看起来在数组的每个条目中只有1个。 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 , . 最棘手的部分是date.parse不会对“12”或“1”的工作,所以,我们不得不暂时取代“日”或“ST”了, This way, date.parse works on the string. 这样, date.parse可以处理字符串。

 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. 来自@adeneo的评论: Date.parse是依赖于实现的。 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. 作为一种更确定的方法,您可以使用类似moment.js的东西进行日期解析。

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. Kevbot答案中的解决方案很优雅,但其应用程序仅限于ES6浏览器,其实现的date.parse()符合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: 为了避免使用date.parse()依赖项而不是添加诸如moment.js之类的库,可以使用几行代码来制作适用于任何JavaScript环境(包括旧浏览器)的定制解决方案:

 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]}, 

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

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