Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8529861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:07:31+00:00 2026-06-11T09:07:31+00:00

var array = [3,9,23,76,1,54,21,12,0,9,2]; var shuffled = array.sort(function() {return 0.5 – Math.random()}); console.log(shuffled); I’m

  • 0
var array = [3,9,23,76,1,54,21,12,0,9,2];
var shuffled = array.sort(function() {return 0.5 - Math.random()});
console.log(shuffled);  

I’m aware of the results, and also satified with them.

The code above returns a shuffled order of the elements of my array.

I’m puzzled by why it results in that output.

What’s the point of the function inside of the .sort and how does it contribute to the output?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:07:32+00:00Added an answer on June 11, 2026 at 9:07 am

    The function actually takes two arguments, which are 2 items from the array. The purpose is to compare these elements and return a number.
    If the number is positive, the second item should be before the first item.
    If the number is 0 or negative, the second item should be after the first item.

    [0,1].sort(function(a,b){return  1;}); // [1, 0], reverses order
    [0,1].sort(function(a,b){return  0;}); // [0, 1], does nothing
    [0,1].sort(function(a,b){return -1;}); // [0, 1], does nothing 
    

    In each case of the example above, a === 0 and b === 1.


    Edit for step by step output

    To see step-by-step what is happening with an ascending sort on [1,3,2,4,4,0], one can write a function which logs exactly what happens each step

    arr = [1,3,2,4,4,0];
    arr.sort(function(a,b){ // ascending order sort
        var result = a-b,
        str = '';
        if(result > 0) str = 'so swapping';
        else if(result === 0) str = 'so ignoring'
        else str = 'so continuing';
        console.log('with [ '+arr.join(', ')+' ]','comparing',a,'to',b,'resulting in',result, str);
        return result;
    });
    console.log('resulting in [ '+arr.join(', ')+' ]');
    

    which outputs

    with [ 1, 3, 2, 4, 4, 0 ] comparing 1 to 3 resulting in -2 so continuing
    with [ 1, 3, 2, 4, 4, 0 ] comparing 3 to 2 resulting in 1 so swapping
    with [ 1, 3, 3, 4, 4, 0 ] comparing 1 to 2 resulting in -1 so continuing
    with [ 1, 2, 3, 4, 4, 0 ] comparing 3 to 4 resulting in -1 so continuing
    with [ 1, 2, 3, 4, 4, 0 ] comparing 4 to 4 resulting in 0 so ignoring
    with [ 1, 2, 3, 4, 4, 0 ] comparing 4 to 0 resulting in 4 so swapping
    with [ 1, 2, 3, 4, 4, 4 ] comparing 4 to 0 resulting in 4 so swapping
    with [ 1, 2, 3, 4, 4, 4 ] comparing 3 to 0 resulting in 3 so swapping
    with [ 1, 2, 3, 3, 4, 4 ] comparing 2 to 0 resulting in 2 so swapping
    with [ 1, 2, 2, 3, 4, 4 ] comparing 1 to 0 resulting in 1 so swapping
    resulting in [ 0, 1, 2, 3, 4, 4 ]
    

    For completeness, probability table for shuffle algorithm in original question (estimates, based on 500,000 trials for each index), X is starting index

     x     0     1     2     3     4     5     6     7     8     9    10
     0 |  8.0,  8.0,  6.2,  6.6,  9.2, 10.8,  9.3,  6.6,  6.2,  9.7, 18.8
     1 |  4.5,  4.6,  7.8, 12.2, 16.9, 12.9, 11.4, 10.7,  8.7,  6.1,  3.6
     2 | 15.5, 15.5, 10.3,  5.9,  3.7,  3.8,  5.7,  8.3, 10.6, 11.7,  8.5
     3 | 10.4, 10.3, 13.4, 10.2,  7.0,  6.5,  7.8,  9.4,  9.7,  8.8,  6.0
     4 |  6.4,  6.3, 10.7, 15.4, 11.4,  9.5,  9.6,  9.9,  8.9,  6.9,  4.4
     5 | 16.1, 16.1, 10.9,  7.7,  7.4,  7.6,  6.2,  4.4,  4.1,  6.5, 12.5
     6 |  4.7,  4.7,  7.1,  9.7, 12.6, 16.3, 13.6, 11.9,  9.2,  6.1,  3.6
     7 |  6.0,  6.0,  7.7,  8.9,  9.4, 10.9, 14.0, 13.6, 11.2,  7.4,  4.3
     8 |  8.4,  8.3,  9.1,  8.6,  7.3,  6.7,  8.2, 11.6, 13.8, 10.8,  6.7
     9 | 11.5, 11.4, 10.1,  7.6,  5.0,  3.7,  4.2,  6.7, 10.9, 15.8, 12.6
    10 |  8.0,  8.1,  6.2,  6.6,  9.2, 10.9,  9.2,  6.6,  6.1,  9.8, 18.8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have javascript function return array like this: <script type=text/javascript> function looping() { var
Let's say I have this $(document).ready(function() { var array = $.makeArray($('p')); $(array).appendTo(document.body); }); });
var array = new Array(); $.get('comics.txt', function(data) { array = data.split(,); for(var i =
I have this example: var name; var id; var array = []; $.each(data, function(index,
I have an array with random count of elements, for example: var array =
I decided to get shuffled values from array. for that i used this function,
function split(str) { var array = str.split(';'); var test[][] = new Array(); for(var i
How to rewrite the code using jQuery? <script type=text/javascript> window.onload = function(){ var array
I have an array like this var array = [{ order: 3, sub -
I have an array in Javascript: var array = new array(); array[0] = apples;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.