www.webdeveloper.com
Results 1 to 2 of 2

Thread: usage of method sort() at array

  1. #1
    Join Date
    Jun 2013
    Posts
    2

    usage of method sort() at array

    Could any one tell me why the result of below code is: 3,14,14,36,45,52

    function myCompare(x, y)
    {
    if (x << y)
    return -1;
    else if (x === y)
    return 0;
    else
    return 1;
    }
    var myArray = [14,52,3,14,45,36];
    myArray.sort(myCompare);
    alert(myArray);

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    For me the result is 36, 45, 14, 3, 52, 14 (the reverse order) !
    x<<y is not x<y but x shifts x in binary representation y (< 32) bits to the left !
    Then the test return always -1 and give the reverse order !

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles