Sort an array of arrays by date in Javascript

I have an array of arrays whose first field is a date (in string format). I want to sort them by date (after), so I can use it for further calculations.

I identify two tasks on my issue. First, analyze the strings as dates, and then sort them.

a = new Date(Date.parse('1/11/2014 13:42:54'));
console.log(a)

Bring back 11th of Januarythen as I need1st of November

Then, sorting should work as follows:

function compare(a,b) {
  if (a[0] < b[0])
     return -1;
  if (a[0] > b[0])
    return 1;
  return 0;
}

myarray.sort(compare);

So, how can I solve the date problem to make it work on the sort function?

+4
source share
4 answers

Just write down the part of the date and month, replace them and execute Date.parseand new Datehow it

function getFormattedDate(dateString) {
    var result = dateString.replace(/(\d+)\/(\d+)(.*)/, function (m, g1, g2, g3) {
        return g2 + "/" + g1 + g3;
    });

    return new Date(Date.parse(result));
}

console.log(getFormattedDate('1/11/2014 13:42:54'));
// Sat Nov 01 2014 13:42:54 GMT+0000 (GMT)

(\d+)\/(\d+)(.*) , (\d+) , / (escaped as \/), - (\d+) (.*). , g2 g1 ( ).

. , , , Date. Date.parse, ,

function getEpochTime(dateString) {
    var result = dateString.replace(/(\d+)\/(\d+)(.*)/, function (m, g1, g2, g3) {
        return g2 + "/" + g1 + g3;
    });

    return Date.parse(result);
}

function comparator(firstDate, secondDate) {
    return getEpochTime(firstDate) - getEpochTime(secondDate);
}

:

var arr = ['3/11/2014 13:42:54',
    '2/11/2014 13:42:54',
    '1/12/2014 13:42:54',
    '1/11/2014 13:43:54'
];

arr.sort(comparator);

console.log(arr);

[ '1/11/2014 13:43:54',
  '2/11/2014 13:42:54',
  '3/11/2014 13:42:54',
  '1/12/2014 13:42:54' ]
+1

ISO, :

myarray.sort(function (a, b) {
    return (new Date(a[0])).getTime() - (new Date(b[0])).getTime();
});
+1

.js -, String + Format

moment('1/11/2014 13:42:54', 'DD/MM/YYYY HH:mm:ss')

, , ( ):

array_of_arrays = [
  ['1/11/2014 13:42:54', 'val'],
  ['2/11/2014 13:42:54', true]
];

for(array in array_of_arrays){
     epoch = moment(array.shift,'DD/MM/YYYY HH:mm:ss').unix();
     array.unshift(epoch);
 }

new Date(epoch) , Unix Epoch, inbuid Array.sort -

function Comparator(a,b){
 if (a[0] < b[0]) return -1;
 if (a[0] > b[0]) return 1;
 return 0;
}
array_of_arrays.sort(Comparator);

array_of_arrays,

, , , .

+1

:

a = new Date(Date.parse('1/11/2014 13:42:54'));
console.log(a)

11 , 1- `

dd/mm/yyy; Date.parse('1/11/2014 13:42:54') mm/dd/yyyy

:

function parseDate(str) {
    var ds = str.match(/(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)/);
    // Convert to format: mm/dd/yyyy
    return new Date(ds[3], ds[2] - 1, // month is 0-based
                ds[1], ds[4], ds[5], ds[6]);
}

var arr = [parseDate('3/11/2014 13:42:54'),
  parseDate('2/11/2014 13:42:54'),
  parseDate('1/12/2014 13:42:54'),
  parseDate('1/11/2014 13:43:54')
];

arr.sort();

:

function parseDate(str) {
    var ds = str.match(/(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)/);
    // Convert to format: mm/dd/yyyy
    return new Date(ds[3], ds[2] - 1, // month is 0-based
                ds[1], ds[4], ds[5], ds[6]);
}

var arr = ['3/11/2014 13:42:54',
  '2/11/2014 13:42:54',
  '1/12/2014 13:42:54',
  '1/11/2014 13:43:54'
];


arr.sort(function(a, b) {
    return parseDate(a) - parseDate(b);
})
0

All Articles