Cannot read property of undefined in comparator function

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

Cannot read property of undefined in comparator function

问题

这里的问题是你的比较器(loginComparatortasksAmountComparatorfineComparator)返回了布尔值,而不是要求返回负数、零或正数来指示比较结果。在快速排序算法中,比较器通常返回负数表示第一个元素应该在第二个元素之前,零表示它们相等,正数表示第一个元素应该在第二个元素之后。

因此,你应该修改你的比较器函数,以返回适当的比较结果,例如:

  1. function loginComparator(firstParticipant, secondParticipant) {
  2. if (firstParticipant.login < secondParticipant.login) {
  3. return -1;
  4. } else if (firstParticipant.login > secondParticipant.login) {
  5. return 1;
  6. } else {
  7. return 0;
  8. }
  9. }
  10. function tasksAmountComparator(firstParticipant, secondParticipant) {
  11. if (firstParticipant.solved < secondParticipant.solved) {
  12. return -1;
  13. } else if (firstParticipant.solved > secondParticipant.solved) {
  14. return 1;
  15. } else {
  16. return 0;
  17. }
  18. }
  19. function fineComparator(firstParticipant, secondParticipant) {
  20. if (firstParticipant.fine < secondParticipant.fine) {
  21. return -1;
  22. } else if (firstParticipant.fine > secondParticipant.fine) {
  23. return 1;
  24. } else {
  25. return 0;
  26. }
  27. }

这样修改后,你的比较器函数将返回正确的比较结果,适用于快速排序算法。

英文:

I have a quicksort algoritm (Hoare Partition Scheme) for objects of my class and three comparators.

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

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

  1. class Participant {
  2. login;
  3. fine;
  4. solved;
  5. }
  6. /*This function should convert array of strings -
  7. [&#39;alla 4 100&#39;,&#39;gena 6 1000&#39;,&#39;gosha 2 90&#39;,&#39;rita 2 90&#39;,&#39;timofey 4 80&#39;]
  8. Into an array of Participant objects and return their names separated by &#39;\n&#39;
  9. In this format:
  10. gena
  11. timofey
  12. alla
  13. gosha
  14. rita
  15. */
  16. function effectiveSort(membersInfo) {
  17. var sortedMembers = [];
  18. for (let i = 0; i &lt; membersInfo.length; ++i) {
  19. sortedMembers.push(new Participant);
  20. sortedMembers[i].login = membersInfo[i].split(&#39; &#39;)[0];
  21. sortedMembers[i].fine = Number(membersInfo[i].split(&#39; &#39;)[2]);
  22. sortedMembers[i].solved = Number(membersInfo[i].split(&#39; &#39;)[1]);
  23. }
  24. sortedMembers = (quickSort(sortedMembers, 0, sortedMembers.length - 1, loginComparator));
  25. sortedMembers = (quickSort(sortedMembers, 0, sortedMembers.length - 1, fineComparator));
  26. sortedMembers = (quickSort(sortedMembers, 0, sortedMembers.length - 1, tasksAmountComparator));
  27. return sortedMembers.map(a =&gt; a.login).join(&#39;\n&#39;);
  28. }
  29. //Comparators for each class field. That&#39;s where the problem starts
  30. function loginComparator(firstParticipant, secondParticipant) {
  31. return firstParticipant.login &lt; secondParticipant.login;
  32. }
  33. function tasksAmountComparator(firstParticipant, secondParticipant) {
  34. return firstParticipant.solved &gt;= secondParticipant.solved;
  35. }
  36. function fineComparator(firstParticipant, secondParticipant) {
  37. return firstParticipant.fine &gt;= secondParticipant.fine;
  38. }
  39. //Hoare Partition Scheme
  40. function partition(arr, start, end, compar) {
  41. const pivotVal = arr[Math.floor((start + end) / 2)];
  42. while (start &lt;= end) {
  43. while (compar(arr[start], pivotVal)) {
  44. start++;
  45. }
  46. while (compar(pivotVal, arr[end])) {
  47. end--;
  48. }
  49. if (start &lt;= end) {
  50. let temp = arr[start];
  51. arr[start] = arr[end];
  52. arr[end] = temp;
  53. start++;
  54. end--;
  55. }
  56. }
  57. return start;
  58. }
  59. function quickSort(arr, start = 0, end = arr.length - 1, compar) {
  60. if (start &lt; end) {
  61. const index = partition(arr, start, end, compar);
  62. quickSort(arr, start, index - 1, compar);
  63. quickSort(arr, index, end, compar);
  64. }
  65. return arr;
  66. }
  67. //Test data
  68. var real_mebmers = [&#39;alla 4 100&#39;, &#39;gena 6 1000&#39;, &#39;gosha 2 90&#39;, &#39;rita 2 90&#39;, &#39;timofey 4 80&#39;];
  69. var zero_members = [&#39;alla 0 0&#39;, &#39;gena 0 0&#39;, &#39;gosha 0 0&#39;, &#39;rita 0 0&#39;, &#39;timofey 0 0&#39;];
  70. console.log(effectiveSort(real_mebmers));

<!-- end snippet -->

The problem starts in these comparators
return (firstParticipant.solved &gt;= secondParticipant.solved); and return firstParticipant.fine &gt;= secondParticipant.fine;
JS says that.

> return (firstParticipant.solved >= secondParticipant.solved);
> ^ TypeError: Cannot read property 'solved' of undefined

Same goes for fineComparator. If I change these return statements to
return (firstParticipant.solved &gt; secondParticipant.solved); and here return firstParticipant.fine &gt; secondParticipant.fine;
Everthing seems fine. But why I can't check two properties for equality? I checked these properties and they don't have 'undefined' value, but JS somehow sees it...

答案1

得分: 1

我认为你需要这样做:

我调试了你的代码,看起来在迭代时,end 变量可能会变成 -1。

  1. console.log(list[0]); does work
  2. // console.log(list[-1]); // does not work
英文:

I think you need to do this:

  1. while (end &gt;= 0 &amp;&amp; compar(pivotVal, arr[end])) {

I debugged your code and it looks like when it iterates the end variable can become -1.

  1. list = [1,2,3];
  2. console.log(list[0]); does work
  3. // console.log(list[-1]); // does not work

huangapple
  • 本文由 发表于 2023年4月6日 21:35:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950171.html
匿名

发表评论

匿名网友

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

确定