Skip to content Skip to sidebar Skip to footer

Custom Sorting In Angularjs

I have JSON field like {'facebook':'aravind@facebook.com', 'homeAddress':'26, New Street, Blr', 'officeAddress':'31, Old Office Street, Blr',

Solution 1:

From what I understand, you wish to ask how to sort the data by location in the controller. One way would be to use the 'orderBy' filter. You can use it just like you use the 'filter' filter: filterData=$filter('orderBy')(data,'location');

Please check out this plunk for an example of what I am trying to say: http://plnkr.co/edit/BU4DaxlBSNcmRkXG6krc?p=preview

angular.module('app',[])
  .controller('mainCtrl',function($filter){
var data=[{"facebook":"aravind@facebook.com",
                "homeAddress":"26, New Street, Blr",
                "officeAddress":"31, Old Office Street, Blr",
                "city":"Zlr",
    name:"aravind"
        },
        {"facebook":"ashok@facebook.com",
                "homeAddress":"26, New Street, che",
                "officeAddress":"31, Old Office Street, che",
                "city":"che",
    name:"ashok"
        }];

    var modifiedData=$filter('orderBy')(data,'city');
    console.log(modifiedData);
  })

Solution 2:

Your can create filter like this way!!

`bizBrainCommon.filter('test', function () { return function (items, field, sortingValue) { var filtered = [];

if(!field || !sortingValue)
        return items;

    angular.forEach(items, function (item) {
        filtered.push(item);
    });


    filtered.sort(function (a, b) {

        var upA = a[field].toUpperCase();

        if (upA > sortingValue.toUpperCase() || upA < sortingValue.toUpperCase()) return1;

        if (upA == sortingValue.toUpperCase())
            return -1;
    });

    return filtered;
};

});`

HTML will be like:

ng-repeat="(id, user) in users |test:'fName': search"

Hope this will help you!!

Post a Comment for "Custom Sorting In Angularjs"