SoFunction
Updated on 2025-02-28

js implementation list sorted alphabetically

The examples in this article share with you the specific code of the js implementation list sorted by letters for your reference. The specific content is as follows

Knowledge points

: Return the attribute of the parent node of the element
()method
The insertBefore() method inserts a new child node in front of an existing child node.
Syntax:(newnode, existingnode)
newnode node object must. Node object to insert
existingnode node object must. Children before adding a new node.

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Lists are sorted alphabetically</title>
</head>
<body>
<p>Click the button to sort the list alphabetically</p>
<button onclick="sortList()">Sort</button>
<ul >
 <li>Oslo</li>
 <li>Stockholm</li>
 <li>Helsinki</li>
 <li>Berlin</li>
 <li>Rome</li>
 <li>Madrid</li>
</ul>
<script src="../js/lists are sorted alphabetically.js">
 </script>
</body>
</html>
function sortList() {
 var list=("UL");
 var switching=true;
 /*Make a loop*/
 while (switching){
  //Not switch  switching=false;
  var li=("li");
  //Transfer all lists  for(var i=0;i<(-1);i++){
   switching=false;
   //Check whether the next item should be changed to the current item   if (li[i].() > li[i + 1].()) {
    switching=true;
    break;
   }
  }
  //Position exchange  if(switching){
   li[i].(li[i+1],li[i]);
   switching=true;
  }
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.