1

Is there a reason why java.util.Arrays class does not support a compareTo function for different array types, specifically byte[] arrays? I have found a sample implementation here, but I'm mostly wondering if I'm overlooking some common Java class.

EDIT: compareTo does not behave like Arrays.equals but more so like String.compareTo

4
  • Expectedly it does not provide any compareTo() method, since it does not implement the Comparable interface. For a class with only static methods like Arrays it does not make sense in my opinion. Commented Aug 8, 2013 at 8:02
  • What is it you're trying to accomplish? I don't see what the semantic purpose of the proposed compareTo is when applied to an array. Commented Aug 8, 2013 at 8:10
  • @arjacsoh obviously it would be static int compareTo(byte[] a, byte[] b) Commented Jul 18, 2017 at 17:03
  • @chrylis e.g. in order to define a total order on a set of arrays, e.g. in order to put them (or objects containing them) in collections that require such an order Commented Jul 19, 2017 at 10:22

3 Answers 3

3

Following is a generic compare() function for arrays in Java. You can modify for your needs in case you have a different interpretation for what makes one array "greater than" another in non-straightforward scenarios (different lengths, nulls, etc.). It can also be changed easily to work on primitives, such as byte, int, etc.

public static <T extends Comparable<T>> int compareArray(T[] a, T[] b) {
    if (a == b) { // also covers the case of two null arrays. those are considered 'equal'
        return 0;
    }

    // arbitrary: non-null array is considered 'greater than' null array
    if (a == null) {
        return -1; // "a < b"
    } else if (b == null) {
        return 1; // "a > b"
    }

    // now the item-by-item comparison - the loop runs as long as items in both arrays are equal
    int last = Math.min(a.length, b.length);
    for (int i = 0; i < last; i++) {
        T ai = a[i];
        T bi = b[i];

        if (ai == null && bi == null) {
            continue; // two null items are assumed 'equal'
        } else if (ai == null) { // arbitrary: non-null item is considered 'greater than' null item
            return -1; // "a < b"
        } else if (bi == null) {
            return 1; // "a > b"
        }

        int comp = ai.compareTo(bi);
        if (comp != 0) {
            return comp;
        }
    }

    // shorter array whose items are all equal to the first items of a longer array is considered 'less than'
    if (a.length < b.length) {
        return -1; // "a < b"
    } else if (a.length > b.length) {
        return 1; // "a > b"
    }

    // i.e. (a.length == b.length)
    return 0; // "a = b", same length, all items equal
}
Sign up to request clarification or add additional context in comments.

Comments

2

No specific reason, other than code bloating, which always has played a major role in java: the Java installation always been called a large overhead. A compareTo on byte[] would be signed wouldn't it? Would you like an unsigned too? How often would it be used? Maybe nothing for the core Java.

But you have a point: equals could have been implemented using compareTo.

Comments

1

The meaning of compareTo on an array depends on your use case. Thus it is not implemented in java.util.Arrays and because not every array contains comparable objects..

If you want to implement a compareTo Method on an array you have to answer the question:

When is one array less than, equal to or greater than another array?

If the array is a byte[] that is based on an ascii string it might be easy, but then you have a special use case.

Take a look at these questions to understand what I mean:

  1. How would you compare an array of Comparables when you don't know much more about the elements than that they are Comparables?
  2. What kind of information do you get if you know that one array of Comparables is less than another array of Comparables because each element at the same index is less than the other array's element at the same index and what would you do with such information?
  3. What would be the information that you will get if you compare the binary representation of 2 executable files?

I think that the byte[] is a very specific use case, becuse you think of the byte[] as a string of ascii characters, but remember that the byte[] representation of a string depends of the encoding.

1 Comment

You don't have to think of a byte[] as a string of ascii characters in order to compare them. It's just an array of numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.