Q:

Javascript Program to implement Sorting

0

Program to implement Sorting using javascript

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<!DOCTYPE html>
<html>
<body>

<p>Click the button to sort the list alphabetically:</p>
<button onclick="Sorting()">Sort</button>

<ul id="Country">
  <li>Australia</li>
  <li>India</li>
  <li>USA</li>
  <li>Canada</li>
  <li>England</li>
  <li>Paris</li>
</ul>

<script>
function Sorting() {
  var list, i, replace, b, Switch;
  list = document.getElementById("Country");
  replace = true;
  while (replace) {
    replace = false;
    b = list.getElementsByTagName("LI");
   
    for (i = 0; i < (b.length - 1); i++) {
    
      Switch = false;
    
      if (b[i].innerHTML.toLowerCase() > b[i + 1].innerHTML.toLowerCase()) {
       
        Switch= true;
        break;
      }
    }
    if (Switch) {
      
      b[i].parentNode.insertBefore(b[i + 1], b[i]);
      replace = true;
    }
  }
}
</script>

</body>
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now