-2

I have an array. It looks like below:

var num = [2,1,2,6,2,4];

I would like to sort but return index to an array:

var result = [1, 0, 2, 4, 5, 3];

If there is a same number, the smaller index of should be placed first.

1

1 Answer 1

2

You could get all indices and sort with the values of num.

var num = [2, 1, 2, 6, 2, 4],
    indices = [...num.keys()].sort((a, b) => num[a] - num[b]);

console.log(indices); // [1, 0, 2, 4, 5, 3]

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.