Search code examples
angularjsangularjs-filterng-optionsangularjs-ng-optionsangularjs-orderby

AngularJS custom SortBy


I have an array with several objects {id:x , name:y} retrieved from a Java Enum and I need to order them alphabetically, but one of the objects must mandatorily be in the bottom of the <select> comboBox, how could I achieve this result? I used ng-options with orderBy, but, I can't figure out how to put this particular object into the bottom.


Solution

  • The easiest way is to sort in the controller, when you get the data from the server:

    1. find the "special" object that should go to the bottom and remove it from the array
    2. sort the array, using $filter('orderBy')(array, 'name')
    3. push the "special" object to the sorted array
    4. expose the sorted array on the scope and use that array in ng-options