lostgirl
lostgirl

Reputation: 1

javascript - sort inputs alphabetically

I need a user to enter names then list then as they typed it and then alphabetically. I cant seem to get the alphabetical part... please help. Ive tried localsort and arraysort and nothing has worked. I am very very new to java and have looked through tons of answers on here but can't find any answers. Thank you !!!!

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">

<head>
<title>Part4 
</title>
</head>
 
 <body>
  <strong>Name of Students:</strong>
    <ol id="list">
    </ol>
    
    <strong> Name of Students Alphabetically: </strong>
    <ol id="demo2">
    </ol>
    
 <script type = "text/javascript">
    var list = document.getElementById('list');
    
    var item1 = window.prompt("Enter a name of a student:");
      if (item1 != null) {
          var entry = document.createElement('li');
          entry.appendChild(document.createTextNode(item1));
          list.appendChild(entry);       
    }

    var item2 = window.prompt("Name of another student:");
      if (item2 != null) {
          var entry = document.createElement('li');
          entry.appendChild(document.createTextNode(item2));
          list.appendChild(entry);
      }  
      
      
    var item3 = window.prompt("Name of another student:");
      if (item3 != null) {
          var entry = document.createElement('li');
          entry.appendChild(document.createTextNode(item3));
          list.appendChild(entry);
      }  
      
      
    var item4 = window.prompt("Name of another student:");
      if (item4 != null) {
          var entry = document.createElement('li');
          entry.appendChild(document.createTextNode(item4));
          list.appendChild(entry);
      }  
      
      
    var item5 = window.prompt("Name of another student:");
      if (item5 != null) {
          var entry = document.createElement('li');
          var entry = document.createElement('li');
          entry.appendChild(document.createTextNode(item5));
          list.appendChild(entry);
      }  


function sortarray(a, b){
if (a.item < b.item) return -1; 
if (a.item > b.item) return 1; 
return 0; 
}

var=item.sort(sortarray);
console.log(s); 
      
</script>
</body>
</html>

Upvotes: 0

Views: 53

Answers (1)

AmberWagner
AmberWagner

Reputation: 126

Array.prototype.sort() will treat string by lexicographical order:

If compareFunction is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order.

So you can see the snippet here:

var list = document.getElementById('list');
var list_alphabetically = document.getElementById('alphabetically');

var items = []
var item1 = window.prompt("Enter a name of a student:");
if (item1 != null) {
    var entry = document.createElement('li');
    entry.appendChild(document.createTextNode(item1));
    list.appendChild(entry);  
    items.push(item1)
}

var item2 = window.prompt("Name of another student:");
if (item2 != null) {
    var entry = document.createElement('li');
    entry.appendChild(document.createTextNode(item2));
    list.appendChild(entry);
    items.push(item2)
}

var s = items.sort();
console.log(s);

s.forEach(sortedItem => {
  var entry = document.createElement('li');
    entry.appendChild(document.createTextNode(sortedItem));
    list_alphabetically.appendChild(entry);  
})
<strong>Name of Students:</strong>
<ol id="list"></ol>

<strong> Name of Students Alphabetically: </strong>
<ol id="alphabetically"></ol>

Upvotes: 1

Related Questions