0

I want to pass data array that is coming from function to sort. For example :

const DEFAULT_COMPETITORS = [ 'Seamless/Grubhub', 'test'];

DEFAULT_COMPETITORS.sort(function (a, b) {
    return a.toLowerCase().localeCompare(b.toLowerCase());
});

Above is working fine. But I want data from function instead of DEFAULT_COMPETITORS const. I want like below:

My data is coming in getAllCompetitors instead of const.

function getAllCompetitors() {
    $.ajax({
        url: '/salescrm/getTopCompetitorsList',
        type: 'POST',
        success: function(data) {
            console.log('getAllCompetitors data: ',data);
            response(data);
        },
        error: function(data) {
            console.log('data error: ',data);
        }
    });
 }

getAllCompetitors.sort(function (a, b) {
    return a.toLowerCase().localeCompare(b.toLowerCase());
}); 

Hope you guys got.. could any please help me

Thanks in advance,

1

2 Answers 2

1

i hope this will work

function getAllCompetitors() {
    return $.ajax({
        url: '/salescrm/getTopCompetitorsList',
        type: 'POST',
    });
 }


getAllCompetitors()
      .then(res => {
          // you can sort the data 
          let sortedData = res.sort(function (a, b) {
            return a.toLowerCase().localeCompare(b.toLowerCase());
        }); 
        console.log("sortedData once ajax call made the success",sortedData)
      })
      .fail(err= > console.log(err))

Sign up to request clarification or add additional context in comments.

3 Comments

{ "message": "SyntaxError: expected expression, got '>'", "filename": "stacksnippets.net/js", "lineno": 29, "colno": 17 }
you need to try the code in your project , because you are using url key as "/salescrm/" so here it will take the root which is " stackoverflow.com". just try in your domain so it will take the root domain url
happy coding :)
0

Here is a simple example:

Over any array you could use the funcion .sort() to sort by default sorting rules or using .sort with a function to use a custom sorting rules provided by you in the method

Default sorting:

var items = ['baa', 'aaa', 'aba',"Caa"];
var sortedItems = items.sort();
console.log(sortedItems);

Custom sorting:

var items = ['baa', 'aaa', 'aba','Caa'];
var sortedItems = items.sort(function(item1,item2){
     // Locale text case insensitive compare
     return item1.toLowerCase().localeCompare(item2.toLowerCase());
});
console.log(sortedItems);

Mozilla Sort documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.