Advertisement
Coldsewoo

Untitled

Jun 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function sortOnlyPlus(input) {
  2.   const inputArr = input.map((e, i) => {
  3.     return {
  4.       index: i,
  5.       value: e,
  6.     }
  7.   })
  8.   const inputMinus = inputArr.filter(e => e.value < 0)
  9.   const inputPlus = inputArr.filter(e => e.value >= 0)
  10.   const inputMinusIndex = inputMinus.map(e => e.index)
  11.   const inputSorted = inputPlus.sort((a, b) => a.value - b.value)
  12.  
  13.   const result = []
  14.   let index = 0
  15.   for (let i = 0; i < input.length; i++) {
  16.     if (inputMinusIndex.indexOf(i) > -1) {
  17.       result[i] = inputMinus.filter(e => e.index === i)[0]
  18.     } else {
  19.       result[i] = inputSorted[index]
  20.       index += 1
  21.     }
  22.   }
  23.  
  24.   return result.map(e => e.value)
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement