1

I want to order by elements according to date.

<ion-item ng-repeat="item in PortfolioIdeaList | orderBy:'CreatedDate'" style="background-color:rgb(25, 26, 0);" ng-click="ShowIdeaDetails(item.Id)">
           <div class="row">
               <div class="col col-80">
                   <p> {{item.Title}}</p>
                </div>
                <div class="col col-20">
                   <b> {{item.Status}}</b>
                </div>
            </div>
            <div class="row">
               <div class="col col-70">
                    {{item.CreatedDate}}
                </div>
                <div class="col col-30">
                    <i class="icon ion-chevron-right icon-accessory"></i>
                </div>
            </div>

        </ion-item>

I am getting Title,Created Date and Status from webservices in JSON format. Created Date is coming as String format MM/DD/YYYY HH:MM:SS e.g 6/2/1991 2:33:43 PM or 12/23/2015 11:55:12 AM. but its not ordering properly. How to convert string into date and then order it.

RAHUL DEEP
  • 695
  • 3
  • 13
  • 33
  • Best way to do is to get the sorted data from the server. – Nilesh Jul 29 '15 at 10:33
  • possible duplicate of [OrderBy Date values, which are just strings in Angular JS](http://stackoverflow.com/questions/25306216/orderby-date-values-which-are-just-strings-in-angular-js) – hhsadiq Jul 29 '15 at 11:26

2 Answers2

2

With your code, if CreatedDate is a string, your objects will be orderBy alphanumerical order.

If you want to order by date, you can :

  • Convert this property to Date when you get your objects from the server

       new Date(item.CreatedDate)
    
  • Create a custom filter who convert values into Date and order it :

    .filter("customOrderBy", function () {
         return function (input) {
        input.sort(function(a,b){
            return new Date(a.CreatedDate) > new Date(b.CreatedDate);
        });
        return input;
    }
    });
    

    And call it in your template :

    ng-repeat="item in PortfolioIdeaList | customOrderBy"
    

But in all case, you need to convert your strings to YYYY-MM-DD format

Varkal
  • 1,336
  • 2
  • 16
  • 31
  • But what if we have the date coming from database as 2/22/2015 2:33:44 PM – RAHUL DEEP Jul 30 '15 at 03:23
  • You have two solutions : You can create your own parser to create a Date : see example [here](http://stackoverflow.com/a/10430381/3042398) or you can use an helper library like [momentjs](http://momentjs.com/) – Varkal Jul 30 '15 at 07:55
0

Add :false after orderBy works for me

<ion-item ng-repeat="item in PortfolioIdeaList | orderBy:'CreatedDate':false" style="background-color:rgb(25, 26, 0);" ng-click="ShowIdeaDetails(item.Id)">
bklups
  • 300
  • 1
  • 13