1

I am new to angular Filters.

If i want to sort the email Id's which is in ng-repeat by applying a filter. How to do this?

For example :

[email protected], [email protected] , [email protected] and [email protected] are my emails.

I want to get the output as

[email protected] , [email protected], [email protected] , [email protected]

Its like sorting alphabetically and also in ascending order Both in a filter.

I would really appreciate if working code is provided for this.

Thanks in advance. What I should write in OrderBy: "???????"

Code:

<div class="col-md-12 " ng-repeat="e in allEmails | orderBy:'??????'"> 
    //Display the emails here in sorted alphabetically ascending order.
</div>
1
  • 1
    orderBy:'toString()' Commented Jul 17, 2016 at 8:08

3 Answers 3

3

Try this,

var app = angular.module('myapp', []);

app.controller('MainCtrl', function($scope) {
 
  $scope.emails =  ['[email protected]', '[email protected]', '[email protected]', '[email protected]']
  
  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<body ng-app="myapp" ng-controller="MainCtrl">
   
    <ul>
      <li ng-repeat="email in emails |orderBy:'toString()'">
        {{email}}
      </li>
    </ul>
  </body>

Sign up to request clarification or add additional context in comments.

5 Comments

I have a strange problem here. I am exporting the emails from excel file. For some wered reason it is not sorting all the emails. Can anyone please help me on this
Hi Rejs. So do we need to write like this orderBy:natural('????')... What should I place in ?????.. can you please help me with working example. Thank you very much in advance
That is because '@' comes first than any number. You should make a filter to order the string that comes before the '@'
1

Here is another response creating custom filter mySortFilter:

angular
  .module('myapp', [])
  .filter('mySortFilter', function() {
    return function(input) {
      return input.sort();
    }
  })
  .controller('MainCtrl', function($scope) {
    $scope.emails =  ['[email protected]', '[email protected]', '[email protected]', '[email protected]'] 
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myapp" ng-controller="MainCtrl">
  <ul>
     <li ng-repeat="email in emails | mySortFilter">
        {{email}}
     </li>
  </ul>
</body>

Comments

1

orderBy: email
You just have to order by the name of the variable. Ascending is default. If your variable were $scope.email.addresses = []; then you would have to filter like this:

orderBy: email.addresses

1 Comment

As I answered: email

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.