469,315 Members | 1,578 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

sort which is default compare function

in the default sort which is the compare function used?
alphabetical ordination is so:arry.sort();
but which is the default function used?

is there a good link wich example for alphabetical order
and also for multiarray order? (previous post)


which it is the difference between:
function compareNumbers(a, b)
{return a - b;}
and this:
function compare(a, b)
{ if (a < b by) return -1;
else if (a than b ) return 1;
else return 0;
}
May 26 '07 #1
3 4751
artev wrote on 26 mei 2007 in comp.lang.javascript:
which it is the difference between:
function compareNumbers(a, b)
{return a - b;}
and this:
function compare(a, b)
{ if (a < b by) return -1;
if (a < b)
else if (a than b ) return 1;
else if (a b )
else return 0;
}
No difference when used in a numeric .sort(),
as .sort() only looks for positive, zero or negative.

The first sort function should be faster,
especially important with a large array.

The second one also works in a string compare sort.

In the second function the else-es are not neccessary:

function compare(a, b) {
if (a<b) return -1;
if (a>b) return 1;
return 0;
};
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 26 '07 #2
in my previous I have now post a new code that not work
good;
you know where is the problem?
May 26 '07 #3
artev wrote on 26 mei 2007 in comp.lang.javascript:
in my previous I have now post a new code that not work
good;
you know where is the problem?
What and to whom are you talking [about]?

[please always quote on usenet]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 26 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Uwe Mayer | last post: by
4 posts views Thread by its me | last post: by
7 posts views Thread by Christopher Jeris | last post: by
10 posts views Thread by Paul Schneider | last post: by
18 posts views Thread by Doug Bell | last post: by
10 posts views Thread by eiji | last post: by
4 posts views Thread by Gaijinco | last post: by
14 posts views Thread by william.hooper | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.