By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

Please help me understand this simple function

P: n/a
RC
http://www.w3schools.com/jsref/tryit...tryjsref_sort2

You can see above link or read below i copy/paste from above link

<script type="text/javascript">

function sortNumber(a, b)
{
return a - b
}

var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "5"
arr[4] = "1000"
arr[5] = "1"

document.write(arr + "<br />")
document.write(arr.sort(sortNumber))

</script>

Please help me to understand the last line:

arr.sort(sortNumber)

The function sortNumber has TWO parameters a and b pass in.
I just can't understand how the array 0 to 5 pass thire
values into there to sort.

Please explain to me. Thank Q!
Jun 7 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Wed, 07 Jun 2006 13:38:25 -0400, RC wrote:
http://www.w3schools.com/jsref/tryit...tryjsref_sort2

You can see above link or read below i copy/paste from above link

<script type="text/javascript">

function sortNumber(a, b)
{
return a - b
}

var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "5"
arr[4] = "1000"
arr[5] = "1"

document.write(arr + "<br />")
document.write(arr.sort(sortNumber))

</script>

Please help me to understand the last line:

arr.sort(sortNumber)

The function sortNumber has TWO parameters a and b pass in.
I just can't understand how the array 0 to 5 pass thire
values into there to sort.

Please explain to me. Thank Q!


The main work of the sorting is done by the Array.sort() function. Part of
sorting is you need a way of knowing for a pair of elements which one
should be above, and which one should be below. If you were always sorting
by number, or by string order, the sort function wouldn't need any
parameters, and it would be just 'array.sort()'. But this would mean
having lots of different sort functions for different kinds of thing, so
instead it asks you to provide a function which is given a pair of objects
and returns a result saying whether the first is less than, equal to, or
greater than the second, which is what sortNumber does.

sortNumber isn't called by the array itself - it's called repeatedly by
the sort method every time it needs to decide which array element to put
first.

Jun 7 '06 #2

P: n/a
RC wrote:
function sortNumber(a, b)
{
return a - b
}

var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "5"
arr[4] = "1000"
arr[5] = "1"

document.write(arr + "<br />")
document.write(arr.sort(sortNumber))

Please help me to understand the last line:

arr.sort(sortNumber)

The function sortNumber has TWO parameters a and b pass in.
I just can't understand how the array 0 to 5 pass thire
values into there to sort.


In arr.sort(sortNumber), 'sortNumber' is not a function call but just a
reference to the function that had been defined above. The sort()
function will then use the function provided (sortNumber) anytime it
needs to compare two elements (to sort an array, you need to have a way
to compare any two of its elements).

Most of the power of javascript is in this ability to use functions as
real objects, which makes it a very different language from the whole
Algol family, from C to java (and much more pleasant to work with).

To illustrate, take the following:

<script type="text/javascript">

function addTo(anObject)
{
anObject.field='here!';
return anObject;
}

function example(anObject, aFunction)
{
aFunction(anObject);
}

function exampleReturn()
{
return arguments[1](arguments[0]);
}

var o1={};
example(o1, addTo);
document.write(o1.field+'<br>'); // 'here!'

var o2={};
document.write(exampleReturn(o2, addTo).field+'<br>'); // 'here!'

</script>

Notice, in the second example, that 'return' works normally, returning
the value of the function call. In the first example, function 'example'
just ignores the value it receives from the call, returning itself
'undefined'.

On a completely unrelated note, notice that 'exampleReturn' doesn't
define its arguments. Instead, they are accessed through the 'arguments'
object. In practice, when you define the arguments of a function, you're
just giving aliases to the elements of 'arguments'. Among other things,
this allows a function to have a variable number of arguments, even if
they are named:
<script type="text/javascript">

function sumTo(anObject)
{
// Same as 'if(!anObject) anObject={};'
anObject = anObject || {};
anObject.sum=0;
for(var i=1; i<arguments.length; ++i)
anObject.sum += arguments[i];
return anObject;
}

var s1={otherField: 'otherValue'};
sumTo(s1, 1, 2, 3, 4);
document.write(s1.sum+'<br>'); // '10'
document.write(s1.otherField+'<br>'); // 'otherValue'

var s2=sumTo(null, 2, 6);
document.write(s2.sum+'<br>'); // '8'
document.write(s2.otherField+'<br>'); // 'undefined'

var s3=sumTo();
document.write(s3.sum+'<br>'); // '0'

</script>

In the first case, a variable is created, with an initialised field, and
sumTo is called with 4 numbers. They are summed, put in a new 'sum'
field (if there had beena previous one, it would have been overwritten),
and the object is returned.

In the second case, no object is passed to sumTo, only a list of
numbers, so it creates a new object. As there was no 'otherField' to
begin with, writing it to output yields 'undefined'.

The third case is a variant of the second in which nor arguments are
supplied at all, so the function just builds the object, initalises the
sum. the loop stops before it even begins, and the object is returned.

Notice the use of anObject with boolean operators (!, ||, etc).
Javascript typecasts null, undefined, "" and 0 to false.
This can be both handy and tricky.
--
am

laurus : rhodophyta : brezoneg : smalltalk : stargate
Jun 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.