"sort not a function" error in angularjs only in google chrome

11,826

Your response is not an Array. It could be an object with a property that contains the Array (e.g. response.data) or a string version of you array and then you will need to use JSON.parse and convert the string into an Array.

Anyway, sort is on Array's prototype - that means that all the arrays will have that property on them - This is not related to AngularJS or google chrome.

Share:
11,826
akashrajkn
Author by

akashrajkn

Eating stroopwafels in Weesperplein

Updated on June 14, 2022

Comments

  • akashrajkn
    akashrajkn almost 2 years

    I ran into this problem and am unable to figure out why and couldn't find any explanation on google. I have a $http.get method to retrieve some data from a url, on success i am sorting the response. But when I run the page on google chrome, I get the error "TypeError: response.sort is not a function"

    var orderOfGroups = ["alpha", "beta", "gamma", "delta"];
    
    $http.get(url)
    .success( function (response, status, headers, config) {
        response.sort( function(a, b) {
            var aname = orderOfGroups.indexOf(a.team);
            var bname = orderOfGroups.indexOf(b.team);
    
            return bname-aname;
        });
    });
    

    I am getting the error at "response.sort" line. Also this is happening only in google chrome, I tested it in firefox and IE10, and its working fine on these browsers. The json data I am receiving (response) has the following format

    [
        {
            team: "gamma",
            value: "p"
        },
        {
            team: "alpha",
            value: "q"
        },
        ......
    ]
    

    Could you please tell me how I can resolve this?