Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Below is the code:

<script type="text/javascript">

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

var n = ["10", "5", "40", "25", "100", "1"];
document.write(n.sort(sortNumber));

</script>

I found this is w3school with no explain ay all!! Is sortnumber function is formula of sorting a number? What's mean a and b and why it exist? why sortNumber in n.sort(sortNumber) doesn't specify any parameter of a and b?Can anyone explain it to me? Thanks!!

share|improve this question
add comment (requires an account with 50 reputation)

4 Answers

up vote 5 down vote accepted

The JavaScript sort() function may or may not take a parameter.
The parameter would be a function - meaning what function is to be used to assess which of two elements should be before the other.

The n array is made of strings representing numbers.
Doing a simple sort() without a function, an alphabetical order would be used: the result would be

 "1", "10", "25"... "5"

and is not correct.

Providing a function, sortNumber, tells sort to call that function with two elements of the array each time the sort algorithm wants to know which of the two items is before the other.

Thus sortNumber provided with two items, does a numerical operation to return either

  • a negative value, meaning a is before b
  • a positive value, b is before a
  • zero: they're equal in terms of order (order doesn't matter).
share|improve this answer
but i dont know what a and b represent for in the function.. – dramasea Jan 2 '11 at 3:19
a and b are variables that take the values provided by the sort function. This is a convention, you give the name of the function to sort, and create that function that takes two parameters - you can call them what you want, e.g. param1 and param2 . – ring0 Jan 2 '11 at 3:22
add comment (requires an account with 50 reputation)

You need to consider what sort() consumes; sort() consumes a function that defines the sort oder. To simplify:

array.sort(sortfunc)

So when you define the function sortNumber, you are actually defining how sort will sort the array.

So if we define a function that's body is defined as:

return a - b;

We are asking to sort by ascending order

If we define a function with body:

return b - a;

We are asking to sort by descending order

Hope this helps

share|improve this answer
add comment (requires an account with 50 reputation)

When you write n.sort(sortNumber), you're passing the sortNumber function to sort.
You aren't calling the sortNumber function, so you don't pass any parameters.

Javascript's sort method takes an optional parameter: a function that compares two elements.
sort will call the function you pass to compare pairs of elements in the array.
The compare function that you supply should take two parameters and return a number indicating which one is bigger.

share|improve this answer
Is the function is a formula to sort a number according accendingly ? – dramasea Jan 2 '11 at 3:13
The function you pass can be anything you want. – SLaks Jan 2 '11 at 3:23
so in the above code, what a and b represent for? – dramasea Jan 2 '11 at 3:27
add comment (requires an account with 50 reputation)

The sort function accepts 2 arguments, a and b, and is supposed to return a number.

  • if a is more than b then it should return any positive number.
  • if a is less than b then it should return any negative number.
  • if a is equals to b then it should return 0.

And a - b produces that number. You can also sort it backwords by using b - a instead. You get the idea.

share|improve this answer
but a and b isnt represent anything in the above code – dramasea Jan 2 '11 at 3:24
1  
You don't really call the sortNumber function. You just give sortNumber to n.sort(), and then n.sort calls it for you. n.sort, or Array.prototype.sort expects that the function you give to it takes 2 values and return the comparison result, as I said above. Then it will sort the array for you. – Thai Jan 2 '11 at 9:29
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.