Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort() method

Status
Not open for further replies.

Jonnn

Programmer
Nov 17, 2004
135
GB
when using the sort method

my_array.sort(simple_sort)

simple_sort is a function so why do you not put ...

my_array.sort(simple_sort()) ???

JavaScript Beginner at work :)
 
my_array.sort(simple_sort());
passes the return value of simple_sort to my_array.sort.

my_array.sort(simple_sort); passes the function itself.

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
oh i see, i have never came accross this before, i always thought when placing a function in anywhere it had to have () on the end. Another lesson learnt.

Thanks

JavaScript Beginner at work :)
 
Without the () you refer to the object itself, not the function of the object.

Lee
 
Does this behavior only occur when using the sort() method?

Code:
simple_sort(item1,item2) {
var result = 0;
if(item1 > item2) 
result = 1;
if(item1 < item2) 
result = -1;
if(item1 == item2) 
result = 0;

return result
}

...

Code:
myArr = new Array("Jon","chessbot",cfFlava","trollacious")
myArr.sort(simple_sort)

Where is the sort() method getting the parameters from that are stated in the simple_sort() function???

JavaScript Beginner at work :)
 
The array.sort method is passing the parameters to the simple_sort function, each time it needs to know how to sort two elements.

What you are doing is passing a reference to the function, so that array.sort knows which function to call (these are commonly called "callback functions"). You can do the same thing yourself. I've written code where I passed the name of a function to a routine. I've even written code where the code dynamically created a function name from a variable and a constant, then checked to see if a function by that name existed, and if so passed it to another function. If you ever have to do that, let me know and I'll tell you how I did it. It took me hours to figure out how to test the generated function name to see if a function by that name existed without generating a "function does not exist" error.


Meddle not in the affairs of dragons,
for you are crunchy, and good with mustard.
 
HeHe, playing with javascript can be very rewarding ...

I always wanted to make one of these. Now my day has come :)

Code:
<html>
<head>
<title> Typing A Message </title>

<script language="javascript">

var x = 0;

function type_it() {
setInterval("document.getElementById('paragraph').innerHTML = \"<span style='color:red; font-family:verdana; size:26'>\" + document.getElementById('message').innerHTML.substring(0, x++) + \"</span>\"; if(x > document.getElementById('message').value.length){ x=0; }", 150);
}

</script>

<body>
<div align="center">
<h1> <u> On Screen Typer </u> </h1>
</div>

<br/>

<p id="paragraph">
</p>

<br/><br/>

<div align="center">
<form name="form1">
<textarea name="message" cols="50" rows="5" id="message" value="" onclick="this.select()">Change me </textarea> 

<br/>
<br/>

<input type="button" value="Type message" onclick="type_it()">
</form>
</div>

</body>
</html>

JavaScript Beginner at work :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top