[英]How to sort array of objects by time string value?
I have an array of objects like so : 我有一个像这样的对象数组:
var example = [{
"description": "aaa",
"time": "12:15pm"
}, {
"description": "bbb",
"time": "10:10am"
}, {
"description": "ccc",
"time": "4:00pm"
}, {
"description": "ddd",
"time": "6:15pm"
}, {
"description": "eee",
"time": "1:10am"
}, {
"description": "fff",
"time": "5:00pm"
} ];
I want to sort by the time
value. 我想按
time
值排序。
I have tried to apply this solution which was intended for an array of string values: 我试图应用这个用于字符串值数组的解决方案 :
example.sort(function (a, b) {
return new Date('1970/01/01 ' + a.time) - new Date('1970/01/01 ' + b.time);
});
console.log(example);
I've also been referring to the Mozilla Array.prototype.sort() documentation and tried the following which didn't seem to work: 我也一直在提到Mozilla Array.prototype.sort()文档,并尝试了以下似乎不起作用的:
example.sort(function(a, b) {
if (new Date(a.time) > new Date(b.time)) {
return 1;
}
if (new Date(a.time) < new Date(b.time)) {
return -1;
}
// a must be equal to b
return 0;
});
console.log(example);
The date string that you are generating is not valid so it will always returns current date and time. 您生成的日期字符串无效,因此它将始终返回当前日期和时间。 So generate valid date string(eg :
'1970/01/01 9:34:48 AM'
) then parse and return the difference. 因此生成有效的日期字符串(例如:
'1970/01/01 9:34:48 AM'
)然后解析并返回差异。 Here String#slice()
method can be used to generate the valid date string. 这里
String#slice()
方法可用于生成有效的日期字符串。
var example = [{ "description": "aaa", "time": "12:15pm" }, { "description": "bbb", "time": "10:10am" }, { "description": "ccc", "time": "4:00pm" }, { "description": "ddd", "time": "6:15pm" }, { "description": "eee", "time": "1:10am" }, { "description": "fff", "time": "5:00pm" }]; example.sort(function(a, b) { // get time time from string // then get am or pm from string and append // both can be done using slice method return Date.parse('1970/01/01 ' + a.time.slice(0, -2) + ' ' + a.time.slice(-2)) - Date.parse('1970/01/01 ' + b.time.slice(0, -2) + ' ' + b.time.slice(-2)) }); console.log(example);
You need a space
before am/pm
to make valid date . 在
am/pm
之前需要一个space
来制作有效日期。
We can put it in sort method before comparing as below. 我们可以在比较之前将它放在sort方法中,如下所示。
example.sort(function(a,b){
return new Date('1970/01/01 ' + a.time.replace(/(am|pm)/,' $1'))
- new Date('1970/01/01 ' + b.time.replace(/(am|pm)/,' $1'))
})
You can try to calculate value in 24hr format and sort it accordingly. 您可以尝试以24小时格式计算值并对其进行相应排序。
pm
exists in string pm
12
, add 12 to it 12
,则添加12 var example=[{description:"aaa",time:"12:15pm"},{description:"bbb",time:"10:10am"},{description:"ccc",time:"4:00pm"},{description:"ddd",time:"6:15pm"},{description:"eee",time:"1:10am"},{description:"fff",time:"5:00pm"}]; example.sort(function(a,b){ var t1 = get24HrFormat(a.time); var t2 = get24HrFormat(b.time); return t1>t2 ? 1 : t1<t2 ? -1 : 0; }); function get24HrFormat(str){ var _t = str.split(/[^0-9]/g); _t[0] =+_t[0] + (str.indexOf("pm")>-1 && +_t[0]!==12 ? 12: 0); return _t.join(""); } document.write("<pre>" + JSON.stringify(example,0,4)+ "</pre>")
If you convert your time to hours and minutes (hours should be in 24-hour format) you don't even need to use the Date
constructor. 如果将时间转换为小时和分钟(小时应为24小时格式),则甚至不需要使用
Date
构造函数。
Something like this: 像这样的东西:
example.map(c => {
var time = c.time.substring(0,c.time.length - 2);
var am_pm = c.time.slice(-2);
var hours = parseInt(time.split(':')[0]);
var minutes = parseInt(time.split(':')[1]);
if (hours === 12 && am_pm.toLowerCase() === 'am') {
hours = 0;
} else if (hours < 12 && am_pm.toLowerCase() === 'pm') {
hours += 12;
}
// save hours and minutes
c.hours = hours;
c.minutes = minutes;
return c;
}).sort((a,b) => {
return (a.hours * 100 + a.minutes) - (b.hours * 100 + b.minutes);
});
Note, this modifies the examples
array by adding hours
and minutes
properties. 注意,这会通过添加
hours
和minutes
属性来修改examples
数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.