Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

orderBy dd/mm/yyyy by year angular

Tags:

date

angularjs

The data I am receiving from a webservice is formatted as dd/mm/yyyy. The problem with this is when sorting, it sorts by dd rather than yyyy.

<td ng-repeat="thead in resultHeader">
    <a href="" ng-click="order(thead.line)">{{thead.head}}</a>
    <span class="sortorder" ng-show="predicate === thead.line" ng-class="{reverse:reverse}"></span>
</td>

Controller:

$scope.order = function(predicate) {
    var results = $scope.model.resultList;
    $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
    $scope.predicate = predicate;
    $scope.model.currentPage = 1;
    $scope.model.beginFrom = 0;
};

How can I sort this data by yyyy with my current set up?

{
  "name": "Test",
  "ShipmentDate": "06\/08\/2012"
}
like image 907
developthewebz Avatar asked Mar 15 '23 04:03

developthewebz


2 Answers

The key part is to add a $filter to your module and use that $filter to get the Date value from the string. you can use Date.parse('dd/mm/yyyy') to get the time in float, and then run an Array.sort() to your data.

like image 56
Downhillski Avatar answered Mar 28 '23 13:03

Downhillski


If you converted your date strings to Date objects, you can use the orderBy: filter to sort by date.

<td ng-repeat="thead in resultHeader | orderBy:'ShipmentDate':shouldBeReversedOrder ">
    <a href="" ng-click="shouldBeReversedOrder = !shouldBeReversedOrder">{{thead.head}}</a>
    <span class="sortorder" ng-show="predicate === thead.line" ng-class="{reverse:reverse}"></span>
</td>

When you want to display the date back in a proper formate you can use {{thead.ShipmentDate | 'yyyy-mm-dd'}} to format it for you.

like image 43
Sean Larkin Avatar answered Mar 28 '23 12:03

Sean Larkin