Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

109
Vistas
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>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda