Search code examples
angularjsangularjs-orderby

Custom order sorting in Angular


I have a column as color which having data as

"red","blue","white","black","yellow" 

Now I want to sort them like

"yellow","blue","red","white" and "black"

It means I don't need sorting in alphabetical order.

I want it to customized.

Sample Fiddle

just like you do in mysql: ORDER BY FIELD(color,'yellow','blue','red',"white","black")


Solution

  • In your case you can implement sorting function like this:

    var colors = ['yellow', 'blue', 'red', 'white', 'black'];
    
    $scope.customOrder = function(friend) {
        return colors.indexOf(friend.color);
    };  
    

    Demo: http://jsfiddle.net/f5hb9spz/3/