A
A
Alice Smith2020-04-17 18:13:38
JavaScript
Alice Smith, 2020-04-17 18:13:38

How to sort an array of numbers?

for example:
[2,4,5,6] => [4,5,6,2]
[1,4,5,6,7] => [4,5,6,7,1]
[2, 3,6,7,8,9] => [6,7,8,9,2,3]
[1,2,3,4,8,9,10] => [8,9,10,1 ,2,3,4]

if the current number is < the next one by 2 or more, then put it at the end of the array

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Somewhere Intech, 2020-04-17
@john36allTa

Far from the best option, and the results are not the same as yours, but it meets your condition ..

function doSomethingWith(array){
  let arr = [...array]
  for (let i = 0, end = arr.length - 1; i < end; i++)
    if(arr[i+1] - arr[i] > 1){
        arr.push(arr.splice(i,1)[0])
        i-=2;
      }
  return arr
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question