Skip to main content
Added a comparator version
Source Link

The name 'data' is the best given the two options, as 'arr' is just making it sound like a pirate. We already know it is an array by the type.

I prefer the first code, for all the same reasons jsanc623 said.

In case you wanted a very generic version:

public static <T extends Comparable> boolean isSorted(T[] data) {
    for (int i = 1; i < data.length; i++) {
        if (data[i - 1].compareTo(data[i]) > 0) {
            return false;
        }
    }
    return true;
}

You can use this code along with the version you have, and make other versions for the other primitive types as needed.

I've also seen the i start at 0 and compare the next element instead of the previous.

The reason why it is best to start at 1, is that it is always clear there is an element before, otherwise you have to make your terminating condition be one less - not as elegant.

Adding a Comparator version:

public static <T> boolean isSorted(T[] data, Comparator<T> comparator) {
    for (int i = 1; i < data.length; i++) {
        if (comparator.compare(data[i - 1], data[i]) > 0) {
            return false;
        }
    }
    return true;
}

Which will allow an ordering to be better controlled.

The name 'data' is the best given the two options, as 'arr' is just making it sound like a pirate. We already know it is an array by the type.

I prefer the first code, for all the same reasons jsanc623 said.

In case you wanted a very generic version:

public static <T extends Comparable> boolean isSorted(T[] data) {
    for (int i = 1; i < data.length; i++) {
        if (data[i - 1].compareTo(data[i]) > 0) {
            return false;
        }
    }
    return true;
}

You can use this code along with the version you have, and make other versions for the other primitive types as needed.

I've also seen the i start at 0 and compare the next element instead of the previous.

The reason why it is best to start at 1, is that it is always clear there is an element before, otherwise you have to make your terminating condition be one less - not as elegant.

The name 'data' is the best given the two options, as 'arr' is just making it sound like a pirate. We already know it is an array by the type.

I prefer the first code, for all the same reasons jsanc623 said.

In case you wanted a very generic version:

public static <T extends Comparable> boolean isSorted(T[] data) {
    for (int i = 1; i < data.length; i++) {
        if (data[i - 1].compareTo(data[i]) > 0) {
            return false;
        }
    }
    return true;
}

You can use this code along with the version you have, and make other versions for the other primitive types as needed.

I've also seen the i start at 0 and compare the next element instead of the previous.

The reason why it is best to start at 1, is that it is always clear there is an element before, otherwise you have to make your terminating condition be one less - not as elegant.

Adding a Comparator version:

public static <T> boolean isSorted(T[] data, Comparator<T> comparator) {
    for (int i = 1; i < data.length; i++) {
        if (comparator.compare(data[i - 1], data[i]) > 0) {
            return false;
        }
    }
    return true;
}

Which will allow an ordering to be better controlled.

Post Made Community Wiki
Source Link

The name 'data' is the best given the two options, as 'arr' is just making it sound like a pirate. We already know it is an array by the type.

I prefer the first code, for all the same reasons jsanc623 said.

In case you wanted a very generic version:

public static <T extends Comparable> boolean isSorted(T[] data) {
    for (int i = 1; i < data.length; i++) {
        if (data[i - 1].compareTo(data[i]) > 0) {
            return false;
        }
    }
    return true;
}

You can use this code along with the version you have, and make other versions for the other primitive types as needed.

I've also seen the i start at 0 and compare the next element instead of the previous.

The reason why it is best to start at 1, is that it is always clear there is an element before, otherwise you have to make your terminating condition be one less - not as elegant.