Srinivas Lakshman
Srinivas Lakshman

Reputation: 489

Angular 1.x: OrderBy isn't ordering the dates

How we can use the 'filter' method to order the dates in the ascending order or decending order?

<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> 
</script>
<body>
<div ng-app="myApp" ng-controller="orderCtrl">
<ul><li ng-repeat="x in dates | orderBy">{{x}}</li></ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('orderCtrl', function($scope) {
$scope.dates = ["01/02/2018", "02/02/2018", "06/06/2018", "01/22/2019",     "12/12/2018"];
});
</script>
</body>
</html>

Getting output as 01/02/2018 01/22/2019 02/02/2018 06/06/2018 12/12/2018

Excepted output 01/02/2018 02/02/2018 06/06/2018 12/12/2018 01/22/2019

Upvotes: 0

Views: 36

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222672

You need to convert the string to date and do the sort

<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> 
</script>
<body>
<div ng-app="myApp" ng-controller="orderCtrl">
<ul><li ng-repeat="x in dates | orderBy : sort : false">{{x}}</li></ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('orderCtrl', function($scope) {
$scope.dates = ["01/02/2018", "02/02/2018", "06/06/2018", "01/22/2019","12/12/2018"];
$scope.sort = function(date) {
    var date = new Date(date);
    return date;
};
});
</script>
</body>
</html>

Upvotes: 1

Related Questions