输入:
arr[] = [3, 4, 3, 5, 2, 3, 4, 3, 1, 5]
对数组进行排序后, 数组如下所示
[1(8), 2(4), 3(0), 3(2), 3(5), 3(7), 4(1), 4(6), 5(3), 5(9)]
答案 0 :(得分:0)
您可以将map
与索引一起使用。然后sort
,最后到map
到期望的字符串:
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]})`));
答案 1 :(得分:0)
尝试一下:
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)