How to sort the Jquery url DATE

0

I would like to sort the names of data of 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);
    });
}
    
asked by anonymous 22.08.2017 / 20:30

2 answers

0

Assuming that data is array , you can use the sort() method of JavaScript:

retorno.data.sort(function(a, b){ return a.nome > b.nome; })

Reference: link

Example in JSFiddle: link

    
22.08.2017 / 20:52
0

If you are trying to organize options in alphabetical order, also considering the accents, use this code after the loop :

$(".estadoendereco").html($(".estadoendereco option").sort(function (a,b){
        return (a.text).localeCompare(b.text);
    }));
    
22.08.2017 / 21:22