Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sortOnlyPlus(input) {
- const inputArr = input.map((e, i) => {
- return {
- index: i,
- value: e,
- }
- })
- const inputMinus = inputArr.filter(e => e.value < 0)
- const inputPlus = inputArr.filter(e => e.value >= 0)
- const inputMinusIndex = inputMinus.map(e => e.index)
- const inputSorted = inputPlus.sort((a, b) => a.value - b.value)
- const result = []
- let index = 0
- for (let i = 0; i < input.length; i++) {
- if (inputMinusIndex.indexOf(i) > -1) {
- result[i] = inputMinus.filter(e => e.index === i)[0]
- } else {
- result[i] = inputSorted[index]
- index += 1
- }
- }
- return result.map(e => e.value)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement