简体   繁体   中英

Translating java into javascript using compareTo

I have some code that compares two strings and returns the one with the lowest value, then compares that value to the letterGrade to see which one is the lowest and then returns the higher of the two. My issue is that I am having issues getting this code to work using javascript. Javascript code:

    var result1 = (finalexam.getletterGrade().compareTo(midexam.getletterGrade()));
    var result2 = " ";
    if (result1 == 0)
    {
        result2 = midexam.getletterGrade();
    }
    else if (result1 < 0)
    {
        result2 = midexam.getletterGrade();
    }
    else if (result1 > 0)
    {
        result2 = finalexam.getletterGrade();
    }
    var result3 = (letterGrade.compareTo(result2));
    if (result3 < 0)
    {
        result2 = letterGrade;
    }

JavaScript int doesn't have a ternary compareTo like Java. You could write one. Something like,

function compare(a, b) {
    if (a < b) {
        return -1;
    } else if (a > b) {
        return 1;
    }
    return 0;
}

Then change your compareTo calls to invoke it. Something like

var result1 = compare(finalexam.getletterGrade(), midexam.getletterGrade());

and

var result3 = compare(letterGrade, result2);

my issue was that our backend code sorted our content using Java's compareTo, because of a misunderstanding I attempted to rewrite compareTo in JavaScript, whereas the fix ended up something completely different, however this may solve your issue

String.prototype.compareTo = function (anotherString) {
    var len1 = this.length;
    var len2 = anotherString.length;
    var n = Math.min(len1,len2); //If the lengths will always be the same skip n to be assigned to len1 only, get rid of len2
    var v1 = this;
    var v2= new String(anotherString);
    // You could instead set these vars i and j to passed in offsets if you want to start sorting from, in which case else case will be possible, or only the first i == j if statement will always be run
    var i = 0; 
    var j = 0;

    if (i == j) {
        var k = i;
        var lim = n + i;
        while (k < lim) {
            var c1 = v1[k];
            var c2 = v2[k];
            if (c1 != c2) {
                var returnVal = c1.charCodeAt(0) - c2.charCodeAt(0);
                if (returnVal > 0) 
                    return 1;
                else
                    return -1
                return 0;
            }
            k++;
        }
    } else {
        while (n-- != 0) {
            var c1 = v1[i++];
            var c2 = v2[j++];
            if (c1 != c2) {
                var returnVal = c1.charCodeAt(0) - c2.charCodeAt(0);
                if (returnVal > 0) 
                    return 1;
                else
                    return -1
                return 0;
            }
        }
    }
    return len1 - len2; 
    //return 0; //if lengths are always the same, comment the above line, uncomment this line
}

Call it like below

var a = "whatever";
var b = "however";
a.compareTo(b);

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM