angularjs orderBy and bring item to top

I want an aphebetical list by username and the current user to be displayed at the top. This does the first part of ordering my array of objects alphabetically by username. http://jsfiddle.net/QPndf/. Now to get the current user to display at the top we can do this in a custom filter. Match the current user object and move it to the top of the array. http://jsfiddle.net/422V4/

Update 31/07/2014: Added orderBy:’username’ which sorts the list before moving the current user to top.

Image

 ngHTML

 

 JS


 

 CSS


 

Sam is a passionate web developer, online entrepreneur and investor. In his spare time he enjoys coding, playing chess and reading.

5 thoughts on “angularjs orderBy and bring item to top

  1. In your completed example you’re missing orderBy:’username’ to sort it by name as well as move current user to top. Also in line 5 of your JavaScript there’s a random if ( that never closes

    1. Thanks mate, removed the random if (. Not sure what you mean about the orderBy though it’s in the jsfiddle examples.

      1. What I meant was that the final example says “Custom sort by name, then move current to top” and in the HTML you have data-ng-repeat=”u in users | currentUserToTop:user.id”. This certainly does move current to top, but there is no custom sorting by name. In this example the items are in the same order they are in the controller (except the current is at the top). Changing the HTML to data-ng-repeat=”u in users | orderBy:’username’| currentUserToTop:user.id” sorts the list by name and then moves the current to top.

Leave a Reply

Your email address will not be published. Required fields are marked *