Mike
Mike

Reputation: 143

AngularJS: orderBy

So i have got a ng-repeat with some names in it. What i want to do is following. The names that are outputted to the screen are ordered alphabetically.

<div ng-repeat="n in names | orderBy:'name'"></div>

  • Anne
  • Jane
  • John
  • Mike
  • Ziggy

But is it possible to always show the name 'Mike' as the top name and order the rest alphabetically?

  • Mike
  • Anne
  • Jane
  • John
  • Ziggy

Upvotes: 1

Views: 311

Answers (2)

Yogesh Manware
Yogesh Manware

Reputation: 1843

There is one more way...

add an attribute say order to the user Object which you want to appear at top.

var specificUserName = {name: Mike,order:0} //This name would appear first in the list

HTML would be

<div ng-repeat="n in names | orderBy:['order', 'name']">{{n}}</div>

Upvotes: 0

dfsq
dfsq

Reputation: 193261

You can write additional sorting function for this. For example:

HTML:

<div ng-repeat="n in names | orderBy:[egoSort, 'name']">{{n}}</div>

Controller:

$scope.egoSort = function(name) {
    return name === 'Mike' ? '' : name;
};

This is possible because orderBy allows you to specify several predicates using array notation.

Demo: http://plnkr.co/edit/FfpcjJBBMm52A71Pa01m?p=preview

Upvotes: 4

Related Questions