CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Javascript sorting (http://www.codingforums.com/showthread.php?t=286528)

dojob 01-27-2013 03:00 PM

Javascript sorting
 
First of all could someone explain the difference between
var matchingGame = {}; and
var matchingGame = [];

Which of the following is the correct sorting of these 2 ?
If compareFunction(a, b) is less than 0, sort a to a lower index than b.
or
If compareFunction(a, b) is less than 0, sort b to a lower index than a.

I have found different sources and both of them show different answer
https://developer.mozilla.org/en-US/...cts/Array/sort

Quote:

If compareFunction(a, b) is less than 0, sort a to a lower index than b.
If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.
If compareFunction(a, b) is greater than 0, sort b to a lower index than a.

http://www.wyodor.net/Public/JavaScr...0reference.pdf
Quote:

If compareFunction(a, b)is less than zero, sort b to a lower index than a.
If compareFunction(a, b)returns zero, leave a and b unchanged relative to
each other.
If compareFunction(a, b)is greater than zero, sort b to a higher index than a.

jmrker 01-27-2013 04:20 PM

Are you sorting numbers only or will some elements be some form of strings?

Philip M 01-27-2013 04:43 PM

The two quotes are in effect the same.

Old Pedant 01-28-2013 03:42 AM

Not to ask silly questions, but...Why didn't you just try it?
Code:

<script type="text/javascript">
var arr = [ 31, 72, 13 ];

document.write("Originsl: " + arr + "<hr/>");

arr.sort( function(a,b) { return ( a < b ) ? -1 : 1; } );

document.write("Sort 1: " + arr + "<hr/>");

arr.sort( function(a,b) { return ( a < b ) ? 1 : -1; } );

document.write("Sort 2: " + arr + "<hr/>");
</script>

Clearly the Mozilla.org docs are correct.

Old Pedant 01-28-2013 03:43 AM

Quote:

Originally Posted by Philip M (Post 1309078)
The two quotes are in effect the same.

Hmmm....
quote 1: If compareFunction(a, b) is less than 0, sort a to a lower index than b.
quote 2: If compareFunction(a, b)is less than zero, sort b to a lower index than a.

I am trying real hard to see your viewpoint, Philip, but I can't.

I don't know how the two could be more diametrically opposed.

Old Pedant 01-28-2013 03:53 AM

Quote:

First of all could someone explain the difference between
var matchingGame = {}; and
var matchingGame = [];
Might be better to ask in what ways they are the same. To which the answer is simpler: In no way.

{ } is used to introduce a set of name/value pairs.

As in:
Code:

var sounds = {
    dog : "bark",
    cat : "meow",
    cow : "moo",
    "bald eagle" : "squawk!"
};

Note that you can put quotes around then names, but you don't have to unless they violate standard JavaScript naming conventions.

Also note that the above is entirely equivalent to doing:
Code:

var sounds = new Object();
sounds["dog"] = "bark"
sounds.cat = "meow";
sounds.cow = "moo";
sounds["bald eagle"] = "squawk!";

And all of this means that no matter which way you initialize the object, you can later find the value of on object element via
Code:

    sounds.cat
or
    sounds["cat"]

and so ont.

*************

The square bracket notation is used to create an array of elements (or to declare that a variable is an empty array, if there are no values between the brackets).
Code:

var arr = [ 7, 3, 21, 911 ];
var arr2 = [ "arrays", 3.14159265, "can be", new Date(), "heterogenous" ];
var arr3 = [ ]; // empty array

Array elements are then typically retrieved by element number:
Code:

alert( arr2[1] ); // shows pi
alert( arr2[3] ); // shows time and date when variable was created


felgall 01-28-2013 04:21 AM

Quote:

Originally Posted by Old Pedant (Post 1309167)
3.14159265

That isn't as accurate as JavaScript can handle pi. If you really wanted to use pi in JavaScript you ought to use Math.PI which contains the most accurate value JavaScript can handle - which should be about 15 decimal places rather than 8.

I'd have expected the Mozilla site to have the correct description for sort without needing to test it - they would be the official standard site for JavaScript if they hadn't given that responsibility to ECMA.

Old Pedant 01-28-2013 04:28 AM

Quote:

Originally Posted by felgall (Post 1309174)
That isn't as accurate as JavaScript can handle pi.

Pbbbbbbbbbbbbbbbbbttttttttttttt!

That's me sticking my tongue out at you.

It was an *EXAMPLE*. A very very SILLY example.

I only wanted to make the point that arrays are heterogenous in JavaScript.

Quote:

I'd have expected the Mozilla site to have the correct description...
Agreed. And I knew it was right. My point really was made in my question: "Why didn't you just try it?"

I'm constantly amazed that people will ask a question in forums where they may have to wait hours or days for an answer when they could just try a smidgen of code themselves and know the answer in minutes, at most.

Airblader 01-28-2013 06:56 AM

One point would be that different browsers COULD in theory implement the function differently, but that'd be really silly. Other than that I couldn't agree more. I dont even answer such questions and if i do i just tell them to try it. If you give them the answer they'll just ask again next time and not learn anything.

Philip M 01-28-2013 07:25 AM

Quote:

Originally Posted by Old Pedant (Post 1309166)
Hmmm....
quote 1: If compareFunction(a, b) is less than 0, sort a to a lower index than b.
quote 2: If compareFunction(a, b)is less than zero, sort b to a lower index than a.

I am trying real hard to see your viewpoint, Philip, but I can't.

I don't know how the two could be more diametrically opposed.

Sorry, misread the question. :o


All times are GMT +1. The time now is 05:04 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.