AngularJS | orderBy Filter

The orderBy filter is a handy tool used in angular js.
The orderBy channel encourages you to sort an exhibit.
Of course, it sorts strings in sequential order requests and numbers in the numerical request.

Syntax:

{{ orderBy_expression | orderBy : expression : reverse }} 

Parameter explanation:

expression: It is utilized to decide the request. It very well may be a string, work or a cluster.
invert: It is discretionary. If you want your data to be displayed in reverse order please make sure that reverse condition in parameters to set true.

Examples: Order by case when we go through names

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>  
<html>  
<script src=
</script>  
<body>  
<div ng-app="myApp" ng-controller="orderCtrl">  
<ul>  
<li ng-repeat="x in customers | orderBy : 'name'">  
    {{x.name + ", " + x.city}}  
</li>  
</ul>  
</div>  
<script>  
var app = angular.module('myApp', []);  
app.controller('orderCtrl', function($scope) {  
    $scope.customers = [  
        {"name" : "Amber", "city" : "ajmer"},  
        {"name" : "lakshay ", "city" : "vizag"},  
        {"name" : "karan", "city" : "London"},  
        {"name" : "bhaskar", "city" : "peshawar"},  
            
    ];  
});  
</script>  
<p>The array items are arranged by "name".</p>  
</body>  
</html>  

chevron_right


Output :

output1

Examples: Order by case when we go through gdp by “-” and “+” orderBy

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
  <title>AnngularJS Filters : orderBy </title>
   <script src=
</script>
  <style>
    .tabled{float:left; padding:10px;}
  </style>
 </head>
<body ng-app="orderByDemo">
 <script>
  angular.module('orderByDemo', [])
    .controller('orderByController', ['$scope', function($scope) {
      $scope.countries =
          [{name:'SPAIN', states:'78', gdp:5},
           {name:'FRANCE', states:'46', gdp:4},
           {name:'PORTUGAL', states:'53', gdp:3},
           {name:'CHILE', states:'06', gdp:2},
           {name:'RUSSIA', states:'21', gdp:1}];
    }]);
</script>
<div ng-controller="orderByController">
   
  <table class="tabled">
    <tr>
      <th>Name</th>
      <th>No of States</th>
      <th>GDP(descending)</th>
    </tr>
    <!-- orderBy Descending (-) -->
    <tr ng-repeat="country in countries | orderBy:'-gdp'"
      <td>{{country.name}}</td>
      <td>{{country.states}}</td>
      <td>{{country.gdp}}</td>
    </tr>
  </table>
    
   <table class="tabled">
    <tr>
      <th>Name</th>
      <th>No of States</th>
      <th>GDP(ascending)</th>
    </tr>
    <!-- orderBy Ascending (+) -->
    <tr ng-repeat="country in countries | orderBy:'gdp'">  
      <td>{{country.name}}</td>
      <td>{{country.states}}</td>
      <td>{{country.gdp}}</td>
    </tr>
  </table>
</div>
</body>
</html>
  
<strong> Examples: Order by case (seven wonders)

chevron_right


Output :

output example 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<script src=
</script>
<body>
  
<div ng-app="myApplication" ng-controller="orderCtrl">
  
<ul>
<li ng-repeat="x in sevenwonder | orderBy : '-country'">
{{x.name + ", " + x.country}}</li>
</ul>
  
</div>
  
<script>
var application = angular.module('myApplication', []);
application.controller('orderCtrl', function($scope) {
    $scope.sevenwonder = [
        {"name" : "Great Wall of China" ,"country" : "China"},
        {"name" : "Christ the Redeemer Statue", "country" : "Brazil"},
        {"name" : "Machu Picchu", "country" : "peru"},
        {"name" : "Chichen Itza ", "country" : "Mexico"},
        {"name" : "The Roman Colosseum", "country" : "Rome"},
        {"name" : "Taj Mahal", "country" : "India"},
        {"name" : "Petra", "country" : "Jordan"}
    ];
});
</script>
<p>The array items are sorted by "country".</p>
  
</body>
</html>

chevron_right


Output :



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.