vaibhav
vaibhav

Reputation: 784

Sorting the date in JSON object angular JS

I am making an angular application where I get the JSON object as the response. In the response array I have a date element as "lastUpdatedBy" I want to sort the data through angular sort (orderBy) so that i get the latest updated data in the bottom.

For the comparison I am also taking the sysdate from the server in the same JSON object as "sysDate".

please provide me with a solution. as i don't know how to give the expression in the orderBy filter.

Upvotes: 0

Views: 910

Answers (1)

Mike Gledhill
Mike Gledhill

Reputation: 29191

There's a decent example of using orderBy here:

AngularJS orderBy documentation

You should just be able to do something like this:

<tr ng-repeat="friend in friends | orderBy:'lastUpdatedBy'">
      <td>{{friend.name}}</td>
      <td>{{friend.phone}}</td>
      <td>{{friend.lastUpdatedBy}}</td>
</tr>

Perhaps it'd help if you showed us what orderBy you have tried, which isn't working...?

Update

You can also use this orderBy if you wanted to sort your data in your AngularJS controller, using something like this:

$scope.sortedArray = $filter('orderBy')($scope.unsortedArray, 'lastUpdatedBy', true);

And then you'd be able to get the most-recently updated item using:

$scope.sortedArray[0]

Upvotes: 1

Related Questions