Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have an array of objects, each object contains the following properties:

comments:""
id:1
inProgress:false
jobDescription:null
lastUpdateDate:"07/08/2016"
lastUpdatedByUser:"[email protected]"
product:"Chicken"
status:Object
templateName:"Standard Template"
uploadDate:"07/08/2016 10:36:01"

I need a function in angular that I can loop through the entire list and sort through the uploadDate and have the most recent first, etc.

I tried using this but it did not work:

vm.uploads = $filter('orderBy')(vm.uploads, vm.uploads[0].uploadDate, reverse);
share|improve this question

Array.prototype.sort is enough :

vm.uploads.sort(function(a,b){
   return  new Date(a.uploadDate).getTime()- new Date(b.uploadDate).getTime();
})

If you want desc order , just switch a & b

share|improve this answer

Your have a syntax error, you should call it like this:

vm.uploads = $filter('orderBy')(vm.uploads, 'uploadDate', true)

Take a look on this simple demo:

(function() {
  angular
    .module('app', [])
    .controller('MainCtrl', MainCtrl);

  function MainCtrl($filter) {
    var vm = this;

    vm.reverse = false;
    vm.uploads = [  
       {  
          "comments":"",
          "id":1,
          "inProgress":false,
          "jobDescription":null,
          "lastUpdateDate":"07/08/2016",
          "lastUpdatedByUser":"[email protected]",
          "product":"Chicken",
          "status":1,
          "templateName":"Standard Template",
          "uploadDate":"07/08/2016 10:36:01"
       },
       {  
          "comments":"",
          "id":2,
          "inProgress":true,
          "jobDescription":null,
          "lastUpdateDate":"08/08/2016",
          "lastUpdatedByUser":"[email protected]",
          "product":"Horse",
          "status":1,
          "templateName":"Custom Template",
          "uploadDate":"08/08/2016 12:49:05"
       }
    ];

    vm.order = function () {
      vm.uploads = $filter('orderBy')(vm.uploads, 'uploadDate', vm.reverse = !vm.reverse);
    }
  }
})();
<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
</head>

<body ng-controller="MainCtrl as main">
  <ul>
    <li ng-repeat="upload in main.uploads track by $index" ng-bind="upload.uploadDate"></li>
  </ul>
  <hr>
  <button type="button" value="order" ng-click="main.order()">Change order</button>
</body>

</html>

I'd recommend you to check the docs.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.