5

I have an array like this one:

let array = [14, 42, 1, 3]

And I would like to get the arrays number mapped to this:

[1, 0, 3, 2]

Here is the reason:

  • 1: because 14 is the second biggest number
  • 0: because 42 is the biggest number
  • 3: ...

What I have tried so far:

let sort = (array) => {
  let result = []
  let x = array.slice(0).sort((a, b) => b - a)
  for (let elem of x) {
    result.push(array.indexOf(elem))
  }
  console.log(result)
}

// Working
sort([14, 42, 1, 3]) // [1, 0, 3, 2]

// Not working, includes the index "0" two times
sort([14, 42, 14, 3]) // [1, 0, 0, 3]
// Expected: [1, 0, 2, 3]

2
  • Something like [ 1, 0, 2, 3] Commented Jul 9, 2019 at 18:14
  • can you explain the logic a little bit? Commented Jul 9, 2019 at 20:08

6 Answers 6

9

You could take the indices and sort them by taking the value from the given array.

const sort = array => [...array.keys()].sort((a, b) => array[b] - array[a]);

console.log(sort([14, 42, 1, 3]));
console.log(sort([14, 42, 14, 3]));

Sign up to request clarification or add additional context in comments.

1 Comment

this is a cool solution, thanks this will help me in the future with similar cases.
2

It's because indexOf stops when it finds it's first result.

You could try to change the value to null once it's located the first time, or compare value to values already in the result and ignore those values.

let sort = (array) => {
  let result = []
  let x = array.slice(0).sort((a, b) => b - a)
  for (let elem of x) {
    result.push(array.indexOf(elem))
    array[array.indexOf(elem)] = null;
  }
  console.log(result)
}

Comments

0

let sort = (arr) => {
    let arr2 = arr.slice().sort((a, b) => b - a);
    return arr.map((val) => {
        return arr2.indexOf(val);
    })
}
console.log(sort([14, 42, 1, 3]));

Comments

0

You can use a tracker object, map value and indexes as key/value pair and when looping though the array take the first index from respective key and shift it as well

let sort = (array) => {
  let result = []
  let x = array.slice(0).sort((a, b) => b - a)
  let tracker = x.reduce((op,inp,i)=>{
    op[inp] = op[inp] || []
    op[inp].push(i)
    return op
  },{})
  
  for (let elem of array) {
    let val = tracker[elem][0]
    tracker[elem].shift()
    result.push(val)
  }
  console.log(result)
}

sort([14, 42, 1, 3]) // working

sort([14, 42, 14, 3]) // includes the index "0" two times

Comments

0

enter image description here

(() => {

  function getBiggestOrder (nums) {
    const lookup = {}
    const result = nums.slice(0).sort((a, b) => b - a).map((num, i) => {
      lookup[num] = i
      return num
    })
    
    return nums.map(n => lookup[n])
  }

  const op = getBiggestOrder([14, 42, 1, 3])

  console.log(op)

  return op

})()

Comments

0

You are basically numbering the numbers from biggest to smallest. sort them in a duplicate array from biggest to smallest. And replace the original array numbers with their index in the duplicate array.

Original = [14, 42, 1, 3]
Duplicate = [42, 14, 3, 1]
Duplicate indexes are [0, 1, 2, 3]

so find 42 in the first array and replace it with the index of the 42 in the duplicate array, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.