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
<th>is not allowed as a direct child of<table>