1

I have the following array of objects in my controller

$scope.records = [
    {
      "fName" : "Alfreds",
      "lName" : "Berglunds",
      "country" : "Germany",
      "age":21
    },
    {   
      "fName" : "Berglunds",
      "lName" : "Alfreds",
      "country" : "Sweden",
      "age":22
    },
    {      
      "fName" : "Centro",
      "lName" : "Ernst",
      "country" : "Mexico",
      "age":23
    },
    {      
      "fName" : "Ernst",
      "lName" : "Centro",
      "country" : "Austria",
      "age":24
    }
  ]

And I am populating a table in my view using the above array

<table ng-controller="myCtrl" border="1">
  <th ng-click="sortByFirstName()">First Nmae</th>
  <th ng-click="sortByLastName()">Last Name</th>
  <th ng-click="sortByCountry()">Country</th>
  <th ng-click="sortByAge()">Age</th>

  <tr ng-repeat="x in records">
    <td>{{x.fName}}</td>
    <td>{{x.lName}}</td>
    <td>{{x.country}}</td>
    <td>{{x.age}}</td>
  </tr>
</table>

On click of each column header, I need to sort the records in ascending order and clicking it again should flip the sorting order(descending order). Right now I am using separate functions to sort them. Can anyone suggest a better way to achieve the same? something like using a generic sort function

1
  • The markup is invalid. <th> is not allowed as a direct child of <table> Commented Dec 28, 2017 at 13:04

2 Answers 2

2

You can make a generic function with angularjs $filter by passing the sortBy as a parameter to the function

DEMO

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

app.controller('myCtrl',function($scope,$filter){
$scope.sortBy = function(sortBy){
    $scope.records = $filter('orderBy')($scope.records, sortBy);
}
$scope.records = [
    {
      "fName" : "Alfreds",
      "lName" : "Berglunds",
      "country" : "Germany",
      "age":21
    },
    {   
      "fName" : "Berglunds",
      "lName" : "Alfreds",
      "country" : "Sweden",
      "age":22
    },
    {      
      "fName" : "Centro",
      "lName" : "Ernst",
      "country" : "Mexico",
      "age":23
    },
    {      
      "fName" : "Ernst",
      "lName" : "Centro",
      "country" : "Austria",
      "age":24
    }
  ];
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="testApp">
<table ng-controller="myCtrl" border="1">
  <th ng-click="sortBy('fName')">First Nmae</th>
  <th ng-click="sortBy('lName')">Last Name</th>
  <th ng-click="sortBy('country')">Country</th>
  <th ng-click="sortBy('age')">Age</th>

  <tr ng-repeat="x in records">
    <td>{{x.fName}}</td>
    <td>{{x.lName}}</td>
    <td>{{x.country}}</td>
    <td>{{x.age}}</td>
  </tr>
</table>
</body>

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

1 Comment

Your solution works even though th is a direct descendant of table.
0

Use this generic sortBy and pass the property name to it

var sortBy = ( prop ) => $scope.records.sort(  function( a, b ){
   return typeof a == "number" ? ( a[ prop ] - b[ prop ] ) : a[ prop ].localeCompare( b[ prop ] );
});

For example

sortBy ( "fName" );

1 Comment

@Sajeetharan I am not aware of angularjs, hence posting only vanilla js solution.

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.