var anArray = [ 5, 4, 8 , 1, 3] ;
anArray.sort(function (a,b){return a - b});
1) Can someone run me through how JavaScript will execute the sort method with function that is passed as a parameter ?
It will compare 5 with 4 then because it's positive, 4 will be before 5. Then it will compare 5 with every other number but 1 and 3 are also smaller than 5. So how java script know which position to put them before 5?
then it will compare 4 with every other number and 8 with every other number etc... how java script do this? I want to do it with pen and paper.
2) why the function that is passed as an parameter is nameless ?
thank you.
Aucun commentaire:
Enregistrer un commentaire