Schwertfisch
Schwertfisch

Reputation: 143

Issue with orderby a String value

I'm trying to orderby a value in an ng-repeat but it seems not working.

I recreated the issue in the following codepen: CodePen

N.B: orderBy 'book.contents.date' is not working: 111 is the first one it should be the last one.

<li class="animate-repeat fc-event" ng-repeat="book in books | orderBy:'book.contents.date'| filter:searchText as results track by book.contents.name"  
             id="{{book.id}}">

Upvotes: 1

Views: 48

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222682

The issue above is right now contents.date is string in your data. you need to convert that to an int in order to make it work. The fix would be,

Create a function that will convert the string to an int as follows,

 $scope.sort = function(num) {
    var newNum = parseInt(num.contents.date);
    return newNum;
  };

and call it in the HTML as ,

  <li class="animate-repeat fc-event" ng-repeat="book in books | orderBy : sort : false | filter:searchText as results track by book.contents.name"   id="{{book.id}}">

DEMO CODEPEN

Upvotes: 2

Related Questions