javalexicographic

Lexicographical order - two char array as parameters, looking for a better solution


I have to write a function that takes 2 char[]s and returns:

I'm aware of compareTo() method but this is an assignment, I need to avoid using it. So far, my code is working well, I've done a few tests with different words.

I was wondering if there was another way of doing it, my code doesn't feel optimized, it's long and repetitive:

public static int lexico(char[] word1, char[] word2) {
        int length1 = word1.length;
        int length2 = word2.length;

        if (length1 == length2) {
            for (int i = 0; i < length1; i++) {
                if (word1[i] < word2[i]) {
                    return -1;
                } else if (word1[i] > word2[i]) {
                    return 1;
                } else if (i == length1 - 1) {
                    return 0;
                }
            }
        }

        if (length1 < length2) {
            for (int i = 0; i < length1; i++) {
                if (word1[i] < word2[i]) {
                    return -1;
                } else if (word1[i] > word2[i]) {
                    return 1;
                } else if (i == length1 - 1) {
                    // If I'm here then it means that all of the characters
                    // from 0 to length1-1 are equals
                    // but since length of the first string is shorter than the second,
                    // the first string will be put before the second
                    return -1;
                }

            }
        }

        if (length1 > length2) {
            for (int i = 0; i < length2; i++) {
                if (word1[i] < word2[i]) {
                    return -1;
                } else if (word1[i] > word2[i]) {
                    return 1;
                } else if (i == length1 - 1) {
                    return 1;
                }
            }
        }

        return -999;
    }

public static void main(String[] args) {
    char[] share = { 's', 'h', 'a', 'r', 'e' };
    char[] ship = { 's', 'h', 'i', 'p' };

    System.out.println(lexico(share, ship)); // -1 share is before ship
    System.out.println(lexico(ship, share)); // 1 ship is after share
    System.out.println(lexico(ship, ship)); // 0 same word

}

Solution

  • A couple of notes for you: