[Racine des codes] [Page précédente]

array trie notes

/Html JavaSript/array trie notes.html
<script type="text/javascript">
function fctSortNote(a, b){ /* fct de trie : compare deux éléments du tableau */
  return a.note - b.note;
}

var arr = new Array ( /* tableau d'objets {nom, note} */
    {nom:"pierre", note:18.5}
  , {nom:"julie", note:19}
  , {nom:"paul", note:10}
  , {nom:"sophie", note:15}
  , {nom:"luc", note:14}
)

document.write("<b>Avant</b> :<br>");
for(var i=0; i<arr.length; ++i)
  document.write(arr[i].nom + " : " + arr[i].note + "<br>");

document.write("<b>Après</b> :<br>");
var arrSort = arr.sort(fctSortNote); // trie
for(var i=0; i<arrSort.length; ++i)
  document.write(arrSort[i].nom + " : " + arrSort[i].note + "<br>");

</script>
[edit]