var arr = [9,5,8,2,6,7,1,3]; for(var i = 0; i<arr.length; i++){ for(var j = i+1; j < arr.length; j++){ if(arr[i]>arr[j]){ var b = arr[i]; arr[i] = arr[j]; arr[j] = b; } } }