I`have this type of sting:
12/07/2015|Comment1,11/09/2015|Comment2,31/07/2015|Comment3,30/07/2015|Comment4, 16/07/2015|Comment5,09/07/2015|Comment6,"
I`m trying to achive this result:
09/07/2015|Comment6,12/07/2015|Comment1,16/07/2015|Comment5,30/07/2015|Comment4,31/07/2015|Comment3,11/09/2015|Comment2,
My code so far looks like this :
function rearangeDates(old_order){
var list = old_order.split(',');
list = list
.map( // for each element in the list (each date)
function(val,idx){
// use the first part(before the dot(.)), replace the - with spaces and convert to date
console.log(val.split('|')[0].split("/").join("-"))
return new Date(val.split('|')[0].split("/").join("-").replace( /(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3") );
})
.sort(); // at the end sort the results.
console.log(list)
}
My main objective is to arrange the dates but this function results in :
[ Fri Jul 31 2015 00:00:00 GMT+0300 (FLE Daylight Time), Fri Sep 11 2015 00:00:00 GMT+0300 (FLE Daylight Time) -> This should be at the end of the array since Sep is after Jul, Invalid Date, Sun Jul 12 2015 00:00:00 GMT+0300 (FLE Daylight Time), Thu Jul 09 2015 00:00:00 GMT+0300 (FLE Daylight Time), Thu Jul 16 2015 00:00:00 GMT+0300 (FLE Daylight Time), Thu Jul 30 2015 00:00:00 GMT+0300 (FLE Daylight Time)]
splitcan also take a string as parameter.