Explain this tutorial, please
The above produces the following output:
return a - b
var arr = new Array(6)
arr = "10"
arr = "5"
arr = "40"
arr = "25"
arr = "1000"
arr = "1"
document.write(arr + "<br />")
My question is: the sortNumber function is expecting 2 parameters ; can we call that function without passing any parameter like what it is in the last line? Secondly, how is that function sorting the numbers appropriately? Shouldn't "return a - b" subract b from a and then return the result? I hope I'm not asking any stupid questions. Thanks for any help.
Array's sort function takes a reference to a compare function as a parameter. When you call the sort function it loops through the values contained in the array and compares them using the compare function you specified. It rearranges the array based on the results of the compare function.
The compare function will get two parameters every time it is called. If the first parameter is larger than the second it should return a positive number. If the first parameter is smaller than the second it should return a negative number. If the parameters are equal it should return 0.
Originally Posted by Mongus
Semicolon is optional, but you should use it!
Basically you are wondering what is in the black box.
By default the sort method is set you to compare strings. So if you were to sort that number without the custom function it would return a funny result. Hence why you probably found this code.
Now with this code you are passing a reference to a funciton that is doing subtraction. Now as explained this subtraction is used to do a comparsion. A positive number, negative number, and zero tells the hidden part of the code what is going on for the sort.
Now when we add this reference to a function, we are passing the instructions to the hidden code we do not see. What is going on in the back ground is this (this is a basic idea, not the exact thing!)
Hopefully that shows you what is happening inside that black box a little bit.
return a - b
//fancy way to extend the Array Object (normally you do not overwrite methods that exist!)
Array.prototype.sort = function(fnc)
var _arrOrg = this; //The orginal array that is sent in.
var _arrNew = new Array(_arrOrg); //Create a new array to insert the new values
for(var i=1;i<_arrOrg.length;i++) //loop through the orginal array to get the values
var _inserted = false; //set a flag so we know if we got a match
for(var j=0; j< _arrNew.length;j++)//loop through the values of our new array
var _comp = fnc(_arrOrg[i],_arrNew[j]); //use the custom function specified by the user sending the orginal array value we need to find a new home and the new array we are now sorting
if(_comp<0) //if it is less than the current value
var x0 = _arrNew.splice(j,0,_arrOrg[i]); //add it to the new location
_inserted = true; //set the flag to true
break; //exit this loop since we found the spot
if(!_inserted)_arrNew[_arrNew.length] = _arrOrg[i]; //make sure we found the spot, if not add it to the end of our new array
return _arrNew; //return the newly sorted array back
var arr = new Array(6);
arr = "10";
arr = "5";
arr = "40";
arr = "25";
arr = "1000";
arr = "1";
document.write(arr + "<br />");
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)