How to sort an array of objects by the value of a time string?

I have an array of such objects:

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 time value.

I 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 also referred to the Mozilla Array.prototype.sort () documentation and tried the following that didn't seem to work:

 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); 
+5
source share
4 answers

The date string you are generating is invalid, so it always returns the current date and time. So, create the correct date string (ex: '1970/01/01 9:34:48 AM' ), then '1970/01/01 9:34:48 AM' and return the difference. Here, the String#slice() method can be used to create a valid date string.

 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); 
+2
source

To make a valid date, you need space before am/pm .

We can put it in the sorting method before comparison, as shown below.

 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')) }) 
+1
source

You can try to calculate the value in 24-hour format and sort it accordingly.

Logics:

  • Check if pm exists in line
  • If yes and the hour is not 12 , add 12 to it
  • Else returns the number

 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>") 
0
source

If you convert your time into hours and minutes (the clock must be in 24-hour format), you do not even need to use the Date constructor.

Something like that:

 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 the hours and minutes properties.

0
source

All Articles