Created
February 13, 2020 13:08
-
-
Save joe-oli/cd0ee69706376f27de0a2900ffe5a436 to your computer and use it in GitHub Desktop.
sort Array Of Objects By Date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Sort Array of Objects by Date Str yyyy-MM-dd; | |
//to run in node, open cmd prompt enter >node sortArrayOfObjectsByDate.js | |
let myArray = [ | |
{trans_date : '2020-01-03', closing_bal: 298700.70}, | |
{trans_date : '2020-01-07', closing_bal: 654321.20}, | |
{trans_date : '2020-01-06', closing_bal: 111700.70}, | |
{trans_date : '2020-01-01', closing_bal: 3000.70} | |
]; | |
console.log('initial-Array:', myArray); | |
myArray.sort(function compare(a, b) { | |
var dateA = new Date(a.trans_date); | |
var dateB = new Date(b.trans_date); | |
return dateA - dateB; | |
}); | |
console.log('sorted-Array:', myArray); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment