I am attempting to display a series of images in a random order. However, I do not want any single item to repeat until all items have been shown, so instead of selecting a
Click the button to sort the array.
Explanation: normally you have "return (a-b)" yielding a positive number for ascending sort order; or you have "return (b-a)" yielding a negative number for descending sort order.
Here we use Math.random()-0.5 which gives in half of the cases a positive number and in half of the cases a negative number. Thus sorting of pairs is either ascending or descending, yielding a random distribution of the array elements.