Cannot read property of undefined in comparator function

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

Cannot read property of undefined in comparator function

问题

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

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

function loginComparator(firstParticipant, secondParticipant) {
    if (firstParticipant.login < secondParticipant.login) {
        return -1;
    } else if (firstParticipant.login > secondParticipant.login) {
        return 1;
    } else {
        return 0;
    }
}

function tasksAmountComparator(firstParticipant, secondParticipant) {
    if (firstParticipant.solved < secondParticipant.solved) {
        return -1;
    } else if (firstParticipant.solved > secondParticipant.solved) {
        return 1;
    } else {
        return 0;
    }
}

function fineComparator(firstParticipant, secondParticipant) {
    if (firstParticipant.fine < secondParticipant.fine) {
        return -1;
    } else if (firstParticipant.fine > secondParticipant.fine) {
        return 1;
    } else {
        return 0;
    }
}

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

英文:

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

class Participant {
    login;
    fine;
    solved;
}

/*This function should convert array of strings - 
      [&#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;]
      Into an array of Participant objects and return their names separated by &#39;\n&#39;
      In this format:
      gena
      timofey
      alla
      gosha
      rita
*/
function effectiveSort(membersInfo) {
    var sortedMembers = [];
    for (let i = 0; i &lt; membersInfo.length; ++i) {
        sortedMembers.push(new Participant);


        sortedMembers[i].login = membersInfo[i].split(&#39; &#39;)[0];
        sortedMembers[i].fine = Number(membersInfo[i].split(&#39; &#39;)[2]);
        sortedMembers[i].solved = Number(membersInfo[i].split(&#39; &#39;)[1]);
    }
    sortedMembers = (quickSort(sortedMembers, 0, sortedMembers.length - 1, loginComparator));
    sortedMembers = (quickSort(sortedMembers, 0, sortedMembers.length - 1, fineComparator));
    sortedMembers = (quickSort(sortedMembers, 0, sortedMembers.length - 1, tasksAmountComparator));

    return sortedMembers.map(a =&gt; a.login).join(&#39;\n&#39;);
}

//Comparators for each class field. That&#39;s where the problem starts

function loginComparator(firstParticipant, secondParticipant) {
    return firstParticipant.login &lt; secondParticipant.login;
}

function tasksAmountComparator(firstParticipant, secondParticipant) {
    return firstParticipant.solved &gt;= secondParticipant.solved;
}

function fineComparator(firstParticipant, secondParticipant) {

    return firstParticipant.fine &gt;= secondParticipant.fine;
}

//Hoare Partition Scheme

function partition(arr, start, end, compar) {
    const pivotVal = arr[Math.floor((start + end) / 2)];
    while (start &lt;= end) {
        while (compar(arr[start], pivotVal)) {
            start++;
        }
        while (compar(pivotVal, arr[end])) {
            end--;
        }
        if (start &lt;= end) {
            let temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
    }
    return start;
}

function quickSort(arr, start = 0, end = arr.length - 1, compar) {
    if (start &lt; end) {
        const index = partition(arr, start, end, compar);
        quickSort(arr, start, index - 1, compar);
        quickSort(arr, index, end, compar);
    }
    return arr;
}

//Test data
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;];
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;];
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。

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

I think you need to do this:

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.

list = [1,2,3];
console.log(list[0]); does work
// 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:

确定