How to sort the DATA of the Jquery url

Asked

Viewed 54 times

0

I’d like to sort the names of data states in ascending order. How do I do this?

function retornaEstados()
{
    var opcaoCadastro = "";
    $.ajax({
      url: urlBase + "estado",
      method: 'GET'
    }).success(function(retorno)
    {
      retorno.data.forEach(function(item)
      {
        opcaoCadastro = $("<option value=" + item.estado_id + ">" + item.nome + "</option>");
        $(".estadoendereco").append(opcaoCadastro);
      });
    }).error(function(data)
    {
      console.log(data);
    });
}

2 answers

0

0

If you’re looking to organize options in alphabetical order, also considering accents, use this code after the loop:

$(".estadoendereco").html($(".estadoendereco option").sort(function (a,b){
        return (a.text).localeCompare(b.text);
    }));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.