custom order function

2,278 views
Skip to first unread message

Jakub Arnold

unread,
Aug 25, 2012, 4:19:51 PM8/25/12
to ang...@googlegroups.com
I have a ng-repeat with some things, and I want to use a custom function to sort them (basically a function that takes two items and compares them).

I tried something like

<div ng-repeat="item in items | orderBy:foo">

where foo is defined like

$scope.foo = function(a, b) {
  return -1; // tried many values here, but nothing works
}

How would I do this properly in angular?

Pawel Kozlowski

unread,
Aug 25, 2012, 4:25:00 PM8/25/12
to ang...@googlegroups.com
Hi!

It should work OK, you just need to implement your function as described in http://docs.angularjs.org/api/ng.filter:orderBy :

function: Getter function. The result of this function will be sorted using the <=> operator.

So basically you need to return something depending on your (a,b) arguments that will be compared using standard comparison operators. If you return -1 all items are considered as equal so sort has no effect. I could help more if you could share code example with jsFiddle (or at least how your a,b looks like).

Cheers,
Pawel

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

Reply all
Reply to author
Forward
0 new messages