hi all i am using angularjs i have a set of data and get it in one scope variable now my need is i need to sort the getting array value based on date 'MONTH' my Date Format DispenseMonth": "9/1/2016"(MM/DD/YY) this my format here i attached the fiddle now my need is i am getting the array order based on  DispenseMonth -orderby Month help how to do this
            Asked
            
        
        
            Active
            
        
            Viewed 51 times
        
    0
            
            
        - 
                    Possible duplicate of [Sort Javascript Object Array By Date](http://stackoverflow.com/questions/10123953/sort-javascript-object-array-by-date) – Sajal May 22 '17 at 07:56
- 
                    i need to sort by month @Sajal – jose May 22 '17 at 07:58
3 Answers
2
            
            
        Sort using getMonth() from the date object.
var app = angular.module("Filter", []);
app.controller('StateShowCtrl', ['$scope', '$http',   
function ($scope, $http) {   
 
 var data=[
  {
    "PatientState": "AK",
    "DispenseMonth": "7/1/2016" 
    
  },
  {
    "PatientState": "AK",
    "DispenseMonth": "8/1/2016" 
  },
  {
    "PatientState": "AK",
    "DispenseMonth": "9/1/2016" 
  },
  {
    "PatientState": "AK",
    "DispenseMonth": "10/1/2016" 
  },
  {
    "PatientState": "AK",
    "DispenseMonth": "11/1/2016" 
  },
  {
    "PatientState": "AK",
    "DispenseMonth": "2/2/2017" 
  },
  {
    "PatientState": "AK",
    "DispenseMonth": "3/5/2017" 
  }
 ]
 
    $scope.StateInformations =data.sort(function(a, b) {
       return new Date(a.DispenseMonth).getMonth() - new Date(b.DispenseMonth).getMonth();
    });
    console.log( $scope.StateInformations);
}    
]);<!DOCTYPE html>
<html   ng-app="Filter">
<head>
    <!-- basic scripts -->
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body class="no-skin" ng-controller="StateShowCtrl">
</body> 
    
    
        Sajal
        
- 4,359
- 1
- 19
- 39
1
            
            
        Try this:
var app = angular.module("Filter", []);
app.controller('StateShowCtrl', ['$scope', '$http', function ($scope, $http) {     
 var data=[
    {
      "PatientState": "AK",
      "DispenseMonth": "7/1/2016" 
    },
    {
      "PatientState": "AK",
      "DispenseMonth": "8/1/2016" 
    },
    {
      "PatientState": "AK",
      "DispenseMonth": "9/1/2016" 
    },
    {
      "PatientState": "AK",
      "DispenseMonth": "10/3/2016" 
    },
    {
      "PatientState": "AK",
      "DispenseMonth": "11/1/2016" 
    },
    {
      "PatientState": "AK",
      "DispenseMonth": "2/1/2017" 
    },
    {
      "PatientState": "AK",
      "DispenseMonth": "3/1/2017" 
    }]     
 
   $scope.StateInformations =data; 
   //console.log( $scope.StateInformations);   
  
   $scope.add = function(){   
      $scope.StateInformations.push({
          "PatientState": "AK",
          "DispenseMonth": "3/2/2017" 
      });
   }   
}     
]);<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app='Filter' class="no-skin" ng-controller="StateShowCtrl">
      <input type='button' ng-click='add()' value='addNew'/>
      <ul ng-init='item.month=+item.DispenseMonth.split("/")[1]' ng-repeat='item in StateInformations | orderBy:"month"' >
          <li>{{item | json}}</li>
      </ul> 
</div> 
    
    
        Slava Utesinov
        
- 13,410
- 2
- 19
- 26
1
            As I understand, you are only interested on the month for the sorting so you can apply this very short sorting function:
data.sort( (a,b) => a.DispenseMonth.split('/')[0] - b.DispenseMonth.split('/')[0]).
 
    
    
        Slim
        
- 1,924
- 1
- 11
- 20
- 
                    1This answer will not work if later some items will be added or modified, so you should call sort method any time it happens manually. – Slava Utesinov May 22 '17 at 08:20
- 
                    You are right, whenever the array is modified you have to manually sort it again. But I prefer this approach since implementing logic in the templates has huge impact on the performance. Also, having this function you can easily implement a `filter`. – Slim May 22 '17 at 11:22
 
    