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)]
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)]
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]})`));
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)