1

I take a date from json by using ng-repeat in a table. This date looks like this:

20_12_2016

I can't sort it by OrderBy, because it will only sort the first two numbers (20_12_2016 only "20", ordering stops in "_").

Table looks like this:

01_03_2016,
02_01_2016,
02_06_2016...

How can I sort by months?

Mackan
  • 6,200
  • 2
  • 25
  • 45
bafix2203
  • 541
  • 7
  • 25
  • So to me it looks like you have a list of strings (containing formated dates). In this case you might need to create a custom filter doing the ordering for you. It might take the string-data and convert it into a number which can be compared using the default comparison operators. - you can read on creating a filter here: https://docs.angularjs.org/api/ng/filter/filter – Tobi Dec 20 '16 at 14:43
  • An intersting solution would be to recognize in the app `20_12_2016` as a date, so it will be possible to format it. Don't know how for atm. – Mistalis Dec 20 '16 at 14:51
  • Do some research on how to parse dates in JavaScript (or work with the system that's providing the data to give dates in a standard format, like ISO 8601): http://stackoverflow.com/q/2587345/215552 and how to sort by them in angular: http://stackoverflow.com/q/35402478/215552 – Heretic Monkey Dec 20 '16 at 14:54
  • Can you control the format used for the date? If you can have the server supply it YYYY_MM_DD then sorting will 'just work'. Otherwise you will have to provide your own custom comparator. – Duncan Dec 20 '16 at 15:04
  • You don't find a date format anywhere like this. Not a good practice – Mr_Perfect Dec 20 '16 at 15:16

2 Answers2

0

Try this: Plunker

| orderBy: '+date.substring(3,5)'">
0

you can try like

var app = angular.module("myApp", []);
  app.controller("myCtrl", function($scope,$compile) {

   $scope.sortByDate = function(date) {
      var b = date.split('_').map(Number);
      return b
    }
})




 <div ng-repeat="dateobj in ['20_11_2016','25_11_2016','10_11_2016']  | orderBy:getlastname" >{{dateobj}}</div>
kamlesh
  • 238
  • 1
  • 5