Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs orderBy filter does not filter dates correctly

I am having a problem with angularjs orderBy filter. I am trying to sort a list of items by date ascending or descending.

This is how my list looks like

...
<li ng-repeat="issue in content | myStatusFilter : selectedStatuses | orderBy : 'date' : selectedOption" class="toggle-list__row">
    <issue data="issue" />
</li>
...

Content is an array and each issue in content has a date and this date is saved as string. When the selectedOption is true this is what I get

  1. Issue 3 - 31.01.2015
  2. Issue 2 - 21.05.2015
  3. Issue 4 - 10.06.2015
  4. Issue 1 - 04.05.2014 -> should be first in the list actually

and when the selectedOption is false this is the output

  1. Issue 1 - 04.05.2014 - should be the last one
  2. Issue 4 - 10.06.2015
  3. Issue 2 - 21.05.2015
  4. Issue 3 - 31.01.2015

Could you please help me and tell me where is the mistake and how can I fix this issue.

Thx in advance

like image 541
amsalk Avatar asked Apr 15 '26 22:04

amsalk


1 Answers

Your dates are strings, not proper javascript date objects. Since you say you can't fix this on the server, you can atleast fix this in the controller or angular service.

Wherever you get your object that contains dates, loop through the objects and turn them into actual dates, like:

$http.get('something')
     .then(function(result) {
         result.forEach(function(item) {
            item.date = new Date(item.date.replace( /(\d{2}).(\d{2}).(\d{4})/, "$2/$1/$3")
         });
     });
like image 72
Tom Avatar answered Apr 18 '26 11:04

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!