用JS按数组内部的升序值对数组进行排序

huangapple go评论76阅读模式
英文:

Sorting an array by their ascending value within the array in JS

问题

以下是您要翻译的内容的翻译部分:

It's a bit of a tricky situation I'm in, but I have an array like this:

const nums = [32, -3, 62, 8, 121, -231, 62, 13];

and need to replace them by their corresponding ascending index. The above example should yield:

[4, 1, 5, 2, 7, 0, 6, 3]

The solution I've come up with is this:

const nums = [32, -3, 62, 8, 121, -231, 62, 13];
const numsCopy = nums.map(e => e);

// Basic sorting
for (let i = 0; i < numsCopy.length; i++) {
    for (let j = 0; j < numsCopy.length; j++) {
        if (numsCopy[i] < numsCopy[j]) {
            let t = numsCopy[j];
            numsCopy[j] = numsCopy[i];
            numsCopy[i] = t;
        }
    }
}

for (let i = 0; i < numsCopy.length; i++) {
    let sortedValue = numsCopy[i];

    nums[nums.indexOf(sortedValue)] = i;
}

Problems arise however when I change `nums` to include a value `nums.length > n >= 0`. The call `nums.indexOf(...)` may return a faulty result, as it may have already sorted an index, even though it exists somewhere in the array.

If you replace `nums` with these values, `-231` will have an index of `2` for some reason...

const nums = [32, -3, 62, 7, 121, -231, 62, 13, 0];

> [5, 1, 6, 3, 8, 2, 7, 4, 0]

Is there a better approach to this problem, or a fix to my solution?

英文:

It's a bit of a tricky situation I'm in, but I have an array like this:

const nums = [32, -3, 62, 8, 121, -231, 62, 13];

and need to replace them by their corresponding ascending index. The above example should yield:

[4, 1, 5, 2, 7, 0, 6, 3]

The solution I've come up with is this: TS Playground

const nums = [32, -3, 62, 8, 121, -231, 62, 13];
const numsCopy = nums.map(e =&gt; e);
// Basic sorting
for (let i = 0; i &lt; numsCopy.length; i++) {
for (let j = 0; j &lt; numsCopy.length; j++) {
if (numsCopy[i] &lt; numsCopy[j]) {
let t = numsCopy[j];
numsCopy[j] = numsCopy[i];
numsCopy[i] = t;
}
}
}
for (let i = 0; i &lt; numsCopy.length; i++) {
let sortedValue = numsCopy[i];
nums[nums.indexOf(sortedValue)] = i;
}

Problems arise however when I change nums to include a value nums.length &gt; n &gt;= 0. The call nums.indexOf(...) may return a faulty result, as it may have already sorted an index, even though it exists somewhere in the array.

If you replace nums with these values, -231 will have an index of 2 for some reason...

const nums = [32, -3, 62, 7, 121, -231, 62, 13, 0];
&gt; [5, 1, 6, 3, 8, 2, 7, 4, 0]

Is there a better approach to this problem, or a fix to my solution?

答案1

得分: 2

你可以通过值对索引进行排序,并创建一个新的数组,其中包含按排序位置排列的索引值。

要获得所需的结果,请再次调用排序函数,然后你将得到按索引顺序排序的索引。

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

console.log(...fn([32, -3, 62, 8, 121, -231, 62, 13])); // 4 1 5 2 7 0 6 3
console.log(...fn([-1, 3, 1, 0, 2, 9, -2, 7])); // 1 5 3 2 4 7 0 6
英文:

You could sort the indices by the value and create a new array with index values a sorted positions.

to get the wanted result call the sorting function again and you get the indices sorted by the index order.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
sort = array => [...array.keys()].sort((a, b) => array[a] - array[b]),
fn = array => sort(sort(array));

console.log(...fn([32, -3, 62, 8, 121, -231, 62, 13])); // 4 1 5 2 7 0 6 3
console.log(...fn([-1, 3, 1, 0, 2, 9, -2, 7])); // 1 5 3 2 4 7 0 6

<!-- end snippet -->

答案2

得分: 1

以下是代码的翻译部分:

const sortIndicesByValue = array => {
  const sorted = [...array].sort((a, b) => a - b);
  return array.map(e => {
    const i = sorted.indexOf(e);
    sorted[i] = null;
    return i;
  })
}

console.log(...sortIndicesByValue([32, -3, 62, 8, 121, -231, 62, 13]));
console.log(...sortIndicesByValue([-1, 3, 0, 0, 2, 9, -2, 7]));

请注意,这是给定代码的翻译部分,没有其他内容。

英文:

Copy the array, sort its values, get indexOf, and null the value in the sorted copy:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const sortIndicesByValue = array =&gt; {
const sorted = [...array].sort((a, b) =&gt; a - b);
return array.map(e =&gt; {
const i = sorted.indexOf(e);
sorted[i] = null;
return i;
})
}
console.log(...sortIndicesByValue([32, -3, 62, 8, 121, -231, 62, 13]));
console.log(...sortIndicesByValue([-1, 3, 0, 0, 2, 9, -2, 7]));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 01:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定