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

112
Vistas
javascript - ordena las entradas alfabéticamente

Necesito que un usuario ingrese nombres y luego enumere cómo lo escribieron y luego alfabéticamente. Parece que no puedo entender la parte alfabética... por favor ayuda. Probé localsort y arraysort y nada funcionó. Soy muy nuevo en Java y he buscado toneladas de respuestas aquí, pero no puedo encontrar ninguna respuesta. Gracias !!!!

 <!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() tratará la cadena por orden lexicográfico:

Si no se proporciona compareFunction, todos los elementos de matriz no definidos se ordenan convirtiéndolos en cadenas y comparando cadenas en orden de unidades de código UTF-16.

Entonces puedes ver el fragmento aquí:

 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