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

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

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

问题

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

  1. It's a bit of a tricky situation I'm in, but I have an array like this:
  2. const nums = [32, -3, 62, 8, 121, -231, 62, 13];
  3. and need to replace them by their corresponding ascending index. The above example should yield:
  4. [4, 1, 5, 2, 7, 0, 6, 3]
  5. The solution I've come up with is this:
  6. const nums = [32, -3, 62, 8, 121, -231, 62, 13];
  7. const numsCopy = nums.map(e => e);
  8. // Basic sorting
  9. for (let i = 0; i < numsCopy.length; i++) {
  10. for (let j = 0; j < numsCopy.length; j++) {
  11. if (numsCopy[i] < numsCopy[j]) {
  12. let t = numsCopy[j];
  13. numsCopy[j] = numsCopy[i];
  14. numsCopy[i] = t;
  15. }
  16. }
  17. }
  18. for (let i = 0; i < numsCopy.length; i++) {
  19. let sortedValue = numsCopy[i];
  20. nums[nums.indexOf(sortedValue)] = i;
  21. }
  22. 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.
  23. If you replace `nums` with these values, `-231` will have an index of `2` for some reason...
  24. const nums = [32, -3, 62, 7, 121, -231, 62, 13, 0];
  25. > [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:

  1. 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:

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

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

  1. const nums = [32, -3, 62, 8, 121, -231, 62, 13];
  2. const numsCopy = nums.map(e =&gt; e);
  3. // Basic sorting
  4. for (let i = 0; i &lt; numsCopy.length; i++) {
  5. for (let j = 0; j &lt; numsCopy.length; j++) {
  6. if (numsCopy[i] &lt; numsCopy[j]) {
  7. let t = numsCopy[j];
  8. numsCopy[j] = numsCopy[i];
  9. numsCopy[i] = t;
  10. }
  11. }
  12. }
  13. for (let i = 0; i &lt; numsCopy.length; i++) {
  14. let sortedValue = numsCopy[i];
  15. nums[nums.indexOf(sortedValue)] = i;
  16. }

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...

  1. const nums = [32, -3, 62, 7, 121, -231, 62, 13, 0];
  2. &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

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

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

  1. const sort = array => [...array.keys()].sort((a, b) => array[a] - array[b]);
  2. const fn = array => sort(sort(array));
  3. console.log(...fn([32, -3, 62, 8, 121, -231, 62, 13])); // 4 1 5 2 7 0 6 3
  4. 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));

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

<!-- end snippet -->

答案2

得分: 1

以下是代码的翻译部分:

  1. const sortIndicesByValue = array => {
  2. const sorted = [...array].sort((a, b) => a - b);
  3. return array.map(e => {
  4. const i = sorted.indexOf(e);
  5. sorted[i] = null;
  6. return i;
  7. })
  8. }
  9. console.log(...sortIndicesByValue([32, -3, 62, 8, 121, -231, 62, 13]));
  10. 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 -->

  1. const sortIndicesByValue = array =&gt; {
  2. const sorted = [...array].sort((a, b) =&gt; a - b);
  3. return array.map(e =&gt; {
  4. const i = sorted.indexOf(e);
  5. sorted[i] = null;
  6. return i;
  7. })
  8. }
  9. console.log(...sortIndicesByValue([32, -3, 62, 8, 121, -231, 62, 13]));
  10. 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:

确定