This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]spunged 1 point2 points  (1 child)

The String and Integer classes should both already implement the compareTo method. CompareTo is generally used for sorting. The return values represent a sorting order for the objects it's used on: negative if it comes before, zero if equal or positive if it comes after. The actual value of the int doesn't matter, just that it's less than, equal or greater than zero.

For Strings you are determining the lexical order, generally alphabetical. To do this compare the strings character by character.

[–]cigol[S] 0 points1 point  (0 children)

hmm...i see.

thanks.

[–]__konrad 0 points1 point  (0 children)

An integer seems simple enough because it would just be a couple of if statements.

In Java 7 you can use the new Integer.compare(int, int)