comment trier un tableau à n dimensions ?
Bonjour,
j'essaie mais en vain de trier un tableau à n dimension avec la méthode sort()
est-ce possible ?
voila ce que j'avais tenté de faire ...
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
<html>
<head>
<title>Test tri javascript</title>
</head>
<body>
<script>
document.write('<h5>Tentative de tri par la méthode sort()</h5>');
document.write('<h5>Tableau à tier</h5>');
var nb=4; // nombre de lignes et colonnes
//dimensionnement du tableau
var tab1=new Array(nb);
for (i=0;i<nb;i++)
{
tab1[i]=new Array(nb);
}
// remplissage du tableau
for (i=0;i<nb;i++)
for (j=0;j<nb;j++)
{
tab1[i][j]=Math.round(Math.random()*100);
}
function compare(a,b)
{
return(b>a);
}
function affiche()
{
document.write('<table border="2">');
for (i=0;i<nb;i++)
{
document.write('<tr>');
total=0;
for (j=0;j<nb;j++)
{
document.write('<td>');
document.write('<br>',tab1[i][j]);
total=total+tab1[i][j];
document.write('</td>')
}
document.write('<td>total : ',total,'</td>');
document.write('</tr>');
}
document.write('</table>');
}
function trier()
{
tab1.sort(compare);
}
affiche();
document.write('<br> <font color="teal"> pour tester touche <b>"F5"</b></font>');
trier(compare);
document.write('<h5>Tableau sensé être trié.....</h5>');
affiche();
</script>
</body>
</html> |
merci si vous avez une idée