Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

    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
    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.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Question

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

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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
    Last edited by Old Pedant; 01-28-2013 at 03:57 AM.
    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.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,591
    Thanks
    0
    Thanked 644 Times in 634 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

    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.

  • #9
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    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.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •