AngularJS: filter orderBy not working

686 views Asked by At

I'm trying to sort an array of sizes part of an object array(shoes). The orderBy filter does not work. I get the sizes displayed in random order.

<div class="choice" ng-repeat="store in pc.stores track by $index">
  <div class="sizes" <span ng-repeat="size in store.shoes[0].sizes | orderBy:size track by $index">{{size}}</span></div>
</div>

Here's the screen grab of console.log enter image description here

I've also tried the orderBy filter in quotes ('size'). It still doesn't work. I don't know what i'm missing. Thanks.

2

There are 2 answers

0
Nikita On

I have written split as custom filter. Hope this will help you.

<div class="choice" ng-repeat="store in pc.stores track by $index">
  <div class="sizes" <span ng-repeat="size in store.shoes[0].sizes | split track by $index">{{size}}</span></div>
</div>



angular.module('test').filter('split', function () {
    return function (input, delimiter) {
        if (input != undefined) {
            var delimiter = delimiter || ',';
            var d=input.split(delimiter);
            var out=[];
            for(var i=0;i< d.length;i++)
            {
                for(var j=1;j<= d.length ; j++)
                {
                    if(d[i] < d[j])
                    {
                       out.push(d[i]);
                    }else{
                        out.push(d[j]);
                    }
                }
            }

            return out;
        }
    }
});
0
nash63 On

I figured it out. I had to account for float numbers as well so i created a custom function in the controller to sort it

                self.size = function (size) {
                var val = parseFloat(size);
                return isNaN(val) ? size : val;
            };