3gwebtrain
3gwebtrain

Reputation: 15303

Sort is not working properly

I am trying to sort the date by desc wise. But getting wrong result. any one help me? Actually I am getting the date values by "DD/MM/YYYY"

here is my try:

var sortDate = [
  "08/10/2017",
  "08/03/2017",
  "07/04/2017",
  "07/09/2016",
  "07/08/2017",
  "22/07/2017",
  "16/12/2017"
]

sortDate.sort(function(a,b){

  var aComps = a.split("/");
  var bComps = b.split("/");
  
	new Date( bComps[2] +"/"+ bComps[1] +"/"+ bComps[0]) - 
  new Date( aComps[2] +"/"+ aComps[1] +"/"+ aComps[0] )
  
})

console.log(sortDate)

Live Demo

Upvotes: 0

Views: 172

Answers (6)

ifantom
ifantom

Reputation: 1

You could use Date but it slower than simple comparison of strings.

You could use String.prototype.localeCompare() but it much more slower then numbers comparison.

So to make it works and optimize your function you could do:

var dates = [
  "08/10/2017",
  "08/03/2017",
  "07/04/2017",
  "07/09/2016",
  "07/08/2017",
  "22/07/2017",
  "16/12/2017"
]

dates.sort(function (a, b) {
  arA = a.split("/");
  arB = b.split("/");
  
  return Number(arA[2] + arA[1] + arA[0]) - Number(arB[2] + arB[1] + arB[0]);
});

console.log(dates);

Upvotes: 0

Jihed Andolsi
Jihed Andolsi

Reputation: 56

you can try this

var sortDate = [
 "08/10/2017",
 "08/03/2017",
 "07/04/2017",
 "07/09/2016",
 "07/08/2017",
 "22/07/2017",
 "16/12/2017"
]

sortDate.sort(function(a,b){
var aComps = a.split("/");
var bComps = b.split("/");

var date1 = new Date();
var date2 = new Date();
console.log(a)
date1.setMonth(aComps[1]-1)
date1.setFullYear(aComps[2]);
date1.setDate(aComps[0]);
console.log(date1)
console.log(b)
date2.setMonth(bComps[1]-1)
date2.setFullYear(bComps[2]);
date2.setDate(bComps[0]);


console.log(date2)

return date2 - date1;

})

console.log(sortDate)

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68443

Your data is already seems to be having 0 prefix padding so the value of date and month would always be in 2 digits.

So, No need to parse as Dates, just do a string comparison using localeCompare

return  ( aComps[2] + aComps[1] + aComps[0] ).localeCompare( bComps[2] + bComps[1] + bComps[0] );      

Demo

var sortDate = [
  "08/10/2017",
  "08/03/2017",
  "07/04/2017",
  "07/09/2016",
  "07/08/2017",
  "22/07/2017",
  "16/12/2017"
]

sortDate.sort(function(a,b){
    var aComps = a.split("/");
    var bComps = b.split("/");
    return  ( aComps[2] + aComps[1] + aComps[0] ).localeCompare( bComps[2] + bComps[1] + bComps[0] );      
})

console.log(sortDate)

Upvotes: 4

Nina Scholz
Nina Scholz

Reputation: 386883

You could create an ISO 8601 date string and use it with String#localeCompare for sorting.

var sortDate = ["08/10/2017", "08/03/2017", "07/04/2017", "07/09/2016", "07/08/2017", "22/07/2017", "16/12/2017"];

sortDate.sort(function (a, b) {
    function getISO(s) {
        return s.replace(/(\d{2})\/(\d{2})\/(\d{4})/, '$3-$2-$1');
    }

    return getISO(a).localeCompare(getISO(b));
});

console.log(sortDate);

Upvotes: 1

marvel308
marvel308

Reputation: 10458

You can do something like this

var sortDate = [
  "08/10/2017",
  "08/03/2017",
  "07/04/2017",
  "07/09/2016",
  "07/08/2017",
  "22/07/2017",
  "16/12/2017"
]

sortDate.sort(function(a,b){

  var aComps = a.split("/").reverse().join("/");
  var bComps = b.split("/").reverse().join("/");
  
	return new Date(aComps) - 
  new Date(bComps )
  
})

console.log(sortDate)

Upvotes: 0

ricky
ricky

Reputation: 1684

You need to return the value from function :

sortDate.sort(function(a,b){

  var aComps = a.split("/");
  var bComps = b.split("/");

   return  new Date( bComps[2] +"/"+ bComps[1] +"/"+ bComps[0]) - 
  new Date( aComps[2] +"/"+ aComps[1] +"/"+ aComps[0] )

})

Upvotes: 0

Related Questions