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"
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With