user1851003 user1851003 - 4 months ago 22
Javascript Question

sorting of email-ID's using angular Filters

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 :


xyz3@gmail.com, xyz1@gmail.com , xyz2@gmail.com and abc1@gmail.com are my emails.


I want to get the output as


abc1@gmail.com , xyz1@gmail.com, xyz2@gmail.com , xyz3@gmail.com


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>

Answer

Try this,

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

app.controller('MainCtrl', function($scope) {
 
  $scope.emails =  ['xyz3@gmail.com', 'xyz1@gmail.com', 'xyz2@gmail.com', 'abc1@gmail.com']
  
  
});
<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>