0

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

https://jsfiddle.net/kvbgrehr/1/

Neil Lunn
  • 130,590
  • 33
  • 275
  • 280
jose
  • 1,024
  • 9
  • 29
  • 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 Answers3

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,199
  • 1
  • 16
  • 37
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
  • 12,921
  • 2
  • 16
  • 25
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,549
  • 1
  • 9
  • 17
  • 1
    This 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