How to Reorder An Array of Numbers in JavaScript
Sometimes we need to reorder an array of numbers so that the order goes from small to big or otherwise. Logic The logic of reorder an array of numbers is this: Find the min or max number in the array. Push this number into a new array. Delete this number. Continue the second and steps until there is no number in the original array. Tools Here are some of the methods I use to make a reordering function: Math.min() or Math.max() depending whether you want to start with the minimun or maximun number. array.push is used to add number to the array with the target order, small to big or vise versa. array.splice() is used to delete the pushed number to prevent multiple counting. while loop is used to execute the process unitl all numbers are arranged. Code function reorder(arr){ var newArr = []; while(arr.length != 0){ var min = Math.min(...arr); var index = arr...