title: Javascript custom sort function for dates with blanks at the bottom
body: <p>I am using a table plugin (ng-grid) to display a bunch of data which includes some date columns. The plugin allows custom sorting with a "sortingAlgorithm" function, which accepts "aDate" and "bDate" parameters. The dates are stored as strings in the database, and I'm using Moment to convert them to date objects. Here is the sort function:</p>
  
  <pre><code>sortingAlgorithm: function (aDate, bDate) {
      var a = moment(aDate, 'MM/DD/YYYY');
      var b = moment(bDate, 'MM/DD/YYYY');
      if (a.isBefore(b)) {
          return -1;
      }
      else if (a.isAfter(b)) {
          return 1;
      }
      else {
          return 0;
      }
  }
  </code></pre>
  
  <p>This works fine, but if there is no date, just an empty string, the blanks are going at the end of the list when sorted in ascending order, but at the beginning when sorted descending. What can I do to ensure that blanks ("") are always moved to the bottom of the list?</p>
  
  <p>Thanks.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I think this has something to do specifically with the UI-Grid library. See <a href="https://github.com/angular-ui/ui-grid/blob/798cb07b5cf01a71ce68050ffa5a648bbc0a9175/src/js/core/services/rowSorter.js" rel="nofollow">rowSorter.js</a>. It seems to handle nulls internally, which is gumming up my mojo. I also don't see where the selected sort direction is exposed for me to work with...</p>
  
  <p>I'm adding the "angular-ui-grid" tag...</p>