JSFiddle - React, Tailwind, and code Playground

by Slico

JavaScript

const theArr = [1, 3, 5, 2, 4, 6, 7]

const orderArray = function(arr) {
  let swaps = 0
  for (let i = 0; i < arr.length - 1; i++) {

    let temp
    if (arr[i] > arr[i + 1]) {
      temp = arr[i]
      arr[i] = arr[i + 1]
      arr[i + 1] = temp

      swaps++
      i = -1

    }

  }
  return swaps
}

console.log(orderArray(theArr))