-6

Input :

arr[] = [3, 4, 3, 5, 2, 3, 4, 3, 1, 5]

After sorting array by, array as shown below

[1(8), 2(4), 3(0), 3(2), 3(5), 3(7), 4(1), 4(6), 5(3), 5(9)]
vahdet
  • 6,357
  • 9
  • 51
  • 106

2 Answers2

0

You can map the values with the index. Then sort, and finally map to the desire string:

var arr = [3, 4, 3, 5, 2, 3, 4, 3, 1, 5];

console.log(arr
   .map((x, i) => [x, i])
   .sort((a, b) =>  a[0] - b[0])
   .map(x => `${x[0]}(${x[1]})`));
Ziv Ben-Or
  • 1,149
  • 6
  • 15
0

Try this:

const a = [3, 4, 3, 5, 2, 3, 4, 3, 1, 5]
const result =a.map((el, i) => ({ index: i, value: el }))
.sort( (a, b) => a.value - b.value)
.map(({index,value})=>` ${value}(${index})`);

console.log(result)
Ghoul Ahmed
  • 4,446
  • 1
  • 14
  • 23