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>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>