title: javascript sorting array of object according to date in string with some null values
body: <p>I have an array of objects, I want to sort this array according to date which is in string format. The problem is this array can have blank values for a date, the code I am using works for array without blank values, but with blank values, it doesn't return properly sorted array.</p>
  <p>This is array eg-</p>
  <pre><code>  var array =
  [{
      &quot;_id&quot; : &quot;5f6a0c223932cd1f7ae1d48c&quot;,
      &quot;user_id&quot; : &quot;5e82f6d1ca6f2a3b4d2212a1&quot;,
      &quot;current_term_renewal_date&quot; : &quot;Fri Oct 16 2020 00:00:00&quot;
  },
  {
      &quot;_id&quot; : &quot;5f6990b94130056cd6034091&quot;,
      &quot;user_id&quot; : &quot;5e82f6d1ca6f2a3b4d2212a1&quot;,
      &quot;current_term_renewal_date&quot; : &quot;&quot;
   
  },
  {
      &quot;_id&quot; : &quot;5f6a0c2a3932cd1f7ae1d48d&quot;,
      &quot;user_id&quot; : &quot;5e82f6d1ca6f2a3b4d2212a1&quot;,
      &quot;current_term_renewal_date&quot; : &quot;Fri Nov 27 2020 00:00:00&quot;
      
  },
  
  {
      &quot;_id&quot; : &quot;5f6a0c453932cd1f7ae1d48e&quot;,
      &quot;user_id&quot; : &quot;5e82f6d1ca6f2a3b4d2212a1&quot;,
      &quot;current_term_renewal_date&quot; : &quot;Thu Oct 22 2020 00:00:00&quot;
  },
  
  {
      &quot;_id&quot; : &quot;5f6a0c4d3932cd1f7ae1d48f&quot;,
      &quot;user_id&quot; : &quot;5e82f6d1ca6f2a3b4d2212a1&quot;,
      &quot;current_term_renewal_date&quot; : &quot;Thu Nov 19 2020 00:00:00&quot;
    
  },
  
  {
      &quot;_id&quot; : &quot;5f6a0c5a3932cd1f7ae1d490&quot;,
      &quot;user_id&quot; : &quot;5e82f6d1ca6f2a3b4d2212a1&quot;,
      &quot;current_term_renewal_date&quot; : &quot;Thu Dec 03 2020 00:00:00&quot;
      
  },
  
  {
      &quot;_id&quot; : &quot;5f1bd948a1db382540356394&quot;,
      &quot;user_id&quot; : &quot;5e82f6d1ca6f2a3b4d2212a1&quot;,
      &quot;current_term_renewal_date&quot; : &quot;Mon Jul 13 2020 20:30:00&quot;
     
  },
  {
      &quot;_id&quot; : &quot;5f698ca54130056cd603408e&quot;,
      &quot;user_id&quot; : &quot;5e82f6d1ca6f2a3b4d2212a1&quot;,
      &quot;current_term_renewal_date&quot; : &quot;Fri Sep 25 2020 00:00:00&quot;
     
  },
  {
      &quot;_id&quot; : &quot;5f698cae4130056cd603408f&quot;,
      &quot;user_id&quot; : &quot;5e82f6d1ca6f2a3b4d2212a1&quot;, 
      &quot;current_term_renewal_date&quot; : &quot;Thu Sep 24 2020 00:00:00&quot;
     
  },
  {
      &quot;_id&quot; : &quot;5f698cb64130056cd6034090&quot;,
      &quot;user_id&quot; : &quot;5e82f6d1ca6f2a3b4d2212a1&quot;,
      &quot;current_term_renewal_date&quot; : &quot;Wed Sep 23 2020 00:00:00&quot;
     
  },
  
  {
      &quot;_id&quot; : &quot;5f6a0c163932cd1f7ae1d48b&quot;,
      &quot;user_id&quot; : &quot;5e82f6d1ca6f2a3b4d2212a1&quot;, 
      &quot;current_term_renewal_date&quot; : &quot;Sun Nov 08 2020 00:00:00&quot;
    
  }]
  </code></pre>
  <p>The code I am using is -</p>
  <pre><code>  array.sort(function compare(a, b) {
              var dateA = new Date(a.current_term_renewal_date);
              var dateB = new Date(b.current_term_renewal_date);
              return dateA - dateB;
          });
  </code></pre>
  <p>The expected result-</p>
  <pre><code>[{
    _id: &quot;5f1bd948a1db382540356394&quot;,
    current_term_renewal_date: &quot;Mon Jul 13 2020 20:30:00&quot;,
    user_id: &quot;5e82f6d1ca6f2a3b4d2212a1&quot;
  }, {
    _id: &quot;5f698cb64130056cd6034090&quot;,
    current_term_renewal_date: &quot;Wed Sep 23 2020 00:00:00&quot;,
    user_id: &quot;5e82f6d1ca6f2a3b4d2212a1&quot;
  }, {
    _id: &quot;5f698cae4130056cd603408f&quot;,
    current_term_renewal_date: &quot;Thu Sep 24 2020 00:00:00&quot;,
    user_id: &quot;5e82f6d1ca6f2a3b4d2212a1&quot;
  }, {
    _id: &quot;5f698ca54130056cd603408e&quot;,
    current_term_renewal_date: &quot;Fri Sep 25 2020 00:00:00&quot;,
    user_id: &quot;5e82f6d1ca6f2a3b4d2212a1&quot;
  }, {
    _id: &quot;5f6a0c223932cd1f7ae1d48c&quot;,
    current_term_renewal_date: &quot;Fri Oct 16 2020 00:00:00&quot;,
    user_id: &quot;5e82f6d1ca6f2a3b4d2212a1&quot;
  }, {
    _id: &quot;5f6a0c453932cd1f7ae1d48e&quot;,
    current_term_renewal_date: &quot;Thu Oct 22 2020 00:00:00&quot;,
    user_id: &quot;5e82f6d1ca6f2a3b4d2212a1&quot;
  }, {
    _id: &quot;5f6a0c163932cd1f7ae1d48b&quot;,
    current_term_renewal_date: &quot;Sun Nov 08 2020 00:00:00&quot;,
    user_id: &quot;5e82f6d1ca6f2a3b4d2212a1&quot;
  }, {
    _id: &quot;5f6a0c4d3932cd1f7ae1d48f&quot;,
    current_term_renewal_date: &quot;Thu Nov 19 2020 00:00:00&quot;,
    user_id: &quot;5e82f6d1ca6f2a3b4d2212a1&quot;
  }, {
    _id: &quot;5f6a0c2a3932cd1f7ae1d48d&quot;,
    current_term_renewal_date: &quot;Fri Nov 27 2020 00:00:00&quot;,
    user_id: &quot;5e82f6d1ca6f2a3b4d2212a1&quot;
  }, {
    _id: &quot;5f6a0c5a3932cd1f7ae1d490&quot;,
    current_term_renewal_date: &quot;Thu Dec 03 2020 00:00:00&quot;,
    user_id: &quot;5e82f6d1ca6f2a3b4d2212a1&quot;
  }, {
    _id: &quot;5f6990b94130056cd6034091&quot;,
    current_term_renewal_date: &quot;&quot;,
    user_id: &quot;5e82f6d1ca6f2a3b4d2212a1&quot;
  }]
  </code></pre>
  <p>This is the link for jsfiddle
  <a href="https://jsfiddle.net/bvnyc97p/" rel="nofollow noreferrer">JS Fiddle Code</a></p>