i`have 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 achive 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 far looks :
function rearangedates(old_order){ var list = old_order.split(','); list = list .map( // each element in list (each date) function(val,idx){ // use first part(before dot(.)), replace - spaces , convert 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(); // @ end sort results. console.log(list) } my main objective arrange dates 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) -> should @ end of array since sep 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)]
you'll need pass compare function sort:
.sort(function(a, b){ return a.gettime() - b.gettime(); }) that should sort dates in right order.
the sort order 2 passed values based on return value of function:
- if
comparefunction(a, b)less 0, sort a lower index b, i.e. comes first.- if
comparefunction(a, b)returns 0, leave , b unchanged respect each other, sorted respect different elements. note: ecmascript standard not guarantee behaviour, , not browsers (e.g. mozilla versions dating @ least 2003) respect this.- if
comparefunction(a, b)greater 0, sort b lower index a.comparefunction(a, b)must return same value when given specific pair of elements , b 2 arguments. if inconsistent results returned sort order undefined
Comments
Post a Comment