Sorting is a must needed thing in programming, Here we will sort an array in random order using JavaScript. In this case we will use The Fisher Yates Method.

The Fisher Yates Method

The algorithm effectively puts all the elements into a hat; it continually determines the next element by randomly drawing an element from the hat until no elements remai n.

 for (i = arr.length -1; i > 0; i--) {
    j = Math.floor(Math.random() * i)
    k = arr[i];
    arr[i] = arr[j];
    arr[j] = k;
  }

we looped through the array from the last index to first, every time we have created a random index number and swapped between the random index and the loop's i index.

say for example the array is number = [1, 2, 3, 4 , 5, 6, 7, 8, 9, 10]

Now let's log the Sorted array

$ node index.js

Output in Terminal

[ 3, 1, 5, 6, 9, 8, 2, 7, 10, 4 ]

Every time you will get a new randomly sorted array.

Thanks, hope this helps.