custom sort order not working

126 views Asked by At

I think I've read all the questions and answers about custom sorting in ng-repeat but I still can't get my hands on it. What am I doing wrong, what I'm missing?

This my controller code:

$scope.persons = [
 {'name': "Pete", 'eyes': 'green', 'hair': 'blonde', 'part': 1},
 {'name': "Dave", 'eyes': 'blue', 'hair': 'red', 'part': 2},
 {'name': "Derek", 'eyes': 'blue', 'hair': 'blonde', 'part': 2},
 {'name': "Jake", 'eyes': 'brown', 'hair': 'black', 'part': 3},
 {'name': "Jeff", 'eyes': 'brown', 'hair': 'darkbrown', 'part': 4},
];

$scope.sortPersons = function(item){
  switch(item.eyes){
   case 'green': return 2;
   case 'blue': return 3;
   case 'brown': return 1;
 }
}

HTML:

<div ng-repeat="p in persons | orderBy:sortPersons ">
 <p>name: {{p.name}}, eyes: {{p.eyes}}, hair: {{p.hair}}, part: {{p.part}}</p>
</div>

As you'll understand I want to sort on the eyes color in the order brown - green - blue. But my code isn't working that way. Hope someone can give me a solution.

1

There are 1 answers

2
Barouch Kandov On

I have run your code and it's seems to work

that is my order:

name: Jake, eyes: brown, hair: black, part: 3

name: Jeff, eyes: brown, hair: darkbrown, part: 4

name: Pete, eyes: green, hair: blonde, part: 1

name: Dave, eyes: blue, hair: red, part: 2

name: Derek, eyes: blue, hair: blonde, part: 2

what is the result the your getting?