Mohd Kashif

Mohd Kashif

  • 1.3k
  • 566
  • 22.8k

Write sorting for date and name in javascript?

May 2 2019 1:29 AM
How to write sorting for date and name in js?
i have one table. there are 2 column like name and created date
name: ["A", "A9", "A10", "A11", "A3"]
createdDate: ["Apr 2, 2019 3:07 PM", "Apr 10, 2019 9:25 AM", "Apr 30, 2019 6:08 PM", "Apr 10, 2019 8:25 AM", "Apr 2, 2019 8:07 PM"]
and output should like this :
name: ["A", "A3", "A9", "A10", "A11"]
createdDate: ["Apr 2, 2019 3:07 PM", "Apr 2, 2019 8:07 PM","Apr 10, 2019 8:25 AM", "Apr 10, 2019 9:25 AM", "Apr 30, 2019 6:08 PM"]
 
 
I already try using sort method.
.sort(a,b){
var firstEl = (a === undefined) ? a :a.toUpperCase();
var secondEl = (b === undefined) ? b: b.toUpperCase();
if(firstEl === undefined || firstEl === "") {return (1)}
if(secondEl === undefined || secondEl === "") {return (-1)}
if(firstEl === secondEl) {return 0;}
return firstEl < secondEl ? (-1) : (1 );
but its not workign for alhanumeric value 
 

Answers (2)