Sorting Angularjs ng-repeat by date
<p>I am relatively new to AngularJS. Could use some help</p>

<p>I have a table with the following info</p>

<pre><code>&lt;table&gt;
 &lt;tr&gt;
  &lt;th&gt;&lt;span ng-click="sortType = 'first_name'; sortReverse = !sortReverse"&gt;Referral Name&lt;/span&gt;&lt;/th&gt;
  &lt;th&gt;&lt;span ng-click="sortType = 'date'; sortReverse = !sortReverse"&gt;Referral Name&lt;/span&gt;&lt;/th&gt;
 &lt;/tr&gt;
 &lt;tr ng-repeat="x in referral | orderBy:sortType:sortReverse"&gt;
  &lt;td&gt;name&lt;/td&gt;
  &lt;td&gt;date&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tabe&gt;
</code></pre>

<p>And the js code is as follows (after the controller connections)</p>

<pre><code>$scope.sortType = '';
$scope.sortReverse = false;
</code></pre>

<p>This works perfectly for ascending and descending when sorting the name.</p>

<p>Unfortunately it works similarly in the case of date too (it is sorting alphabetically, rather than by date).</p>

<p>The date format I am getting from the backend(python) is in this format:</p>

<pre><code>i["date"] = i["date"].strftime("%B %d, %Y")
September 13, 2016 &lt;-- this format
</code></pre>

<p>I understand the mistake I made, but I am not able to find the solution for it.</p>

<p>How can I sort by date? </p>

<p>Thanks in advance guys.</p>

<p>As you have noticed, the value you receive is type of string and therefore it is sorted alphabetically. You need to convert it into Date() beforehand. So basically what you need is to loop over the array of data you got and add a new property (or replace existing one) with a new Date object:</p>

<pre><code>referral.forEach((ref) =&gt; {
    ref.date_obj = new Date(ref.date)
};
</code></pre>

<p>I just checked, JavaScript seems to be parsing format "September 13, 2016" pretty well.</p>

python html angularjs sorting