javascript – Sorting li into a div

Question:

Guys, I need to sort a div alphabetically, I use the following strategy, I put it in a div with display: none all read, so after inserting all in the html , I do the while for as long as li that div it will check which one is bigger , the bigger one, identifying it adds in another div , which is like display:block after I remove it from the div with display:none and stay in this loop.

My code is as follows:

while($('#check_presencaNone li').length > '0'){
  $('#check_presencaNone li').each(function(i){
     if($(this).text().toUpperCase() < valor){
       valor = $(this).text().toUpperCase();
       ponteiro = $(this);
     }
  });
  ponteiro.clone().appendTo('#check_presenca');
  ponteiro.remove();   
  console.log($('#check_presencaNone li').length);
}

When running, it's getting in an infinite loop, and I'm not able to identify the error. What is wrong ?

Answer:

The first point that would solve your question would be to change the while code to the one below:

for(i = 0; i <= $('#check_presencaNone li').length; i++) {
  $('#check_presencaNone li').each(function(i){
     if($(this).text().toUpperCase() < valor){
       valor = $(this).text().toUpperCase();
       ponteiro = $(this);
     }
  });
  ponteiro.clone().appendTo('#check_presenca');
  ponteiro.remove();   
  console.log($('#check_presencaNone li').length);
}
Scroll to Top
AllEscort