Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2013, 03:00 PM   PM User | #1
dojob
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
dojob is an unknown quantity at this point
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.
dojob is offline   Reply With Quote
Old 01-27-2013, 04:20 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Question

Are you sorting numbers only or will some elements be some form of strings?
jmrker is offline   Reply With Quote
Old 01-27-2013, 04:43 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
The two quotes are in effect the same.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 01-28-2013, 03:42 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-28-2013, 03:43 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Philip M View Post
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-28-2013, 03:53 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 01-28-2013 at 03:57 AM..
Old Pedant is offline   Reply With Quote
Old 01-28-2013, 04:21 AM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,453
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is online now   Reply With Quote
Old 01-28-2013, 04:28 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by felgall View Post
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-28-2013, 06:56 AM   PM User | #9
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 363
Thanks: 3
Thanked 43 Times in 43 Posts
Airblader can only hope to improve
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.
Airblader is online now   Reply With Quote
Old 01-28-2013, 07:25 AM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:27 PM.


Advertisement
Log in to turn off these ads.