|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: May 2012
Messaggi: 22
|
ordinare i numeri con gli array in javascript
ciao a tutti.. sapete dirmi come posso ordinare i numeri ( nel mio caso voti) in modo crescente in javascript utilizzando gli array?? se vi può essere di aiuto ecco il codice:
<html> <head> <script type="text/javascript"> var alunni =new Array(); var voti= new Array(); for(i=0;i<6;i=i+1) { alunni[i]=prompt("Scrivi il nome degli alunni: "); } for(i=0;i<6;i=i+1) { voti[i]=prompt("Scrivi i voti: (nell' ordine con cui hai scritto i nomi) "); } </script> <table border="1"> <tr> <td><B>Voti</B></td> <td><B>Alunni</B></td> </tr> <script type="text/javascript"> i=0; while(i<alunni.length) { document.write("<tr>"); document.write("<td>"+alunni[i]+"</td>"); document.write("<td>"+voti[i]+"</td>"); document.write("</tr>"); i++; } </script> </table> </html> |
|
|
|
|
|
#2 |
|
Member
Iscritto dal: Jul 2009
Città: Milano
Messaggi: 270
|
Ci sono decine di algoritmi di ordinamento e la maggior parte ordinano una sequenza di elementi, come gli array appunto: quicksort, mergesort, insertionsort, bubblesort ...
Se non t'interessano le prestazioni puoi ordinare gli elementi man mano che li ricevi (è equivalente ad insertionsort, come quando giochi a carte e ordini le carte hai in mano)confrontandoli con quelli già inseriti nell'array, che saranno già ordinati.
__________________
AMD PII x4 955 BE | Sapphire HD4850 Vapor-X 1 GB | Samsung SpinPoint F1 500GB | Samsung EcoGreen F4 2TB Gigabyte GA-MA790FXT-UD5P | Fractal Design Define R3 USB3.0 Titanium Grey | CORSAIR 650W CMPSU-650TX Noctua U12P SE2 | 2 x 2GB Kingston 1333 MHz | Samsung SyncMaster P2450 | Samsung SyncMaster T200 |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Esercizio scolastico?
Qui trovi il thread di qualcuno che pare abbia il tuo stesso problema, prova a dargli un'occhiata: http://www.hwupgrade.it/forum/showthread.php?t=2471017
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
#4 |
|
Member
Iscritto dal: Jul 2009
Città: Milano
Messaggi: 270
|
La classe array implementa la funzione sort() .
http://www.w3schools.com/jsref/jsref_sort.asp
__________________
AMD PII x4 955 BE | Sapphire HD4850 Vapor-X 1 GB | Samsung SpinPoint F1 500GB | Samsung EcoGreen F4 2TB Gigabyte GA-MA790FXT-UD5P | Fractal Design Define R3 USB3.0 Titanium Grey | CORSAIR 650W CMPSU-650TX Noctua U12P SE2 | 2 x 2GB Kingston 1333 MHz | Samsung SyncMaster P2450 | Samsung SyncMaster T200 |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 18:43.



















