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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
var listeEleve = new Array();
//Objet en exemple , un élève ^^
function eleve(nom,note,age){
this.nom = nom;
this.note = note;
this.age = age;
}
function createListe(nomEl, noteE1 , ageEl){
listeEleve.push(new eleve(nomEl, noteE1 ,ageEl));
}
function trierNom(){
alert('avant : ' + listeEleve[0].nom);
alert('Après : ' + listeEleve.sort(comparerNom)[0].nom);
}
//On créer un comparer
function comparerNom(a, b) {
var nameA = a.nom.toLowerCase( ); //On met en minuscule pour assurer la concordance
var nameB = b.nom.toLowerCase( );
//Vérification si l'un ou l'autre
if (nameA < nameB) {return -1}
if (nameA > nameB) {return 1}
//Sinon identique
return 0;
}
createListe("zoro",10,13);
createListe("bernard",12,11);
createListe("Arnaud",3,15);
createListe("Mick",15,9);
trierNom();
</script>
</head>
<body>
</body>
</html> |