Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java compareTo for String and Integer arguments

Tags:

java

compareto

I am implementing the bubble sort algorithm and I want it to be able to accept both Integer and String parameters. I cast all input as Strings and use the compareTo method to compare the integers casted as strings to the strings. I am getting an incorrect answer when using compareTo to compare the casted integers. What am I doing wrong?


2 Answers

Integer.compareTo sorts numbers numerically. This is what you want.

String.compareTo sorts strings lexicographically; that is, in alphabetical order.

I remember in Windows 3.1 that the folder of photos from my digital camera was ordered like this: PHOTO1, PHOTO10, PHOTO100, PHOTO2, PHOTO20, PHOTO3, ... and so on. Windows XP sorts them more like you would expect: PHOTO1, PHOTO2, PHOTO3, ... etc. This is because it has special sorting rules for strings that represent numbers.

In lexicographical ordering, each character in one string A is compared to the corresponding character in another string B. For each corresponding character in the two strings:

  • If A's current character is lexicographically less than (comes before in the alphabet) B's character, then A comes before B.
  • If B's character is less than A's character, then B comes before A.
  • If the two characters are the same, then we don't know yet. The next one is checked.
  • If there are no more characters left in one of the strings, then the shorter one comes before the longer one.
  • If there are no more character left in both strings, then they are the same string.

The fourth point here is why you are getting incorrect answers, assuming Eddie's analysis of your problem is correct.

Consider the strings "10" and "2". Lexicographical ordering would look at the first characters of each, '1' and '2' respectively. The character '1' comes before '2' in the character set that Java uses, so it sorts "10" before "2", in the same way that "bare" is sorted before "hare" because 'b' comes before 'h'.

I suggest you cast your strings to integers before sorting. Use Integer.parseString to do this.

like image 186
Iain Samuel McLean Elder Avatar answered Mar 02 '26 23:03

Iain Samuel McLean Elder


are you sure you want to mix Integers and Strings in the same list? if so, are Integers less or greater than Strings? what is this particular sorting criteria?

you can also make a bubble sort method which sorts distinct lists of Integer and lists of String (and lists of any other class). to do so, you can use Generics. for example:

public static <T> void bubbleSort(List<T> elements, Comparator<T> comparator) {
    // your implementation
}

you use the comparator parameter to compare the elements, that's why they can be Integers or Strings (not both at the same time). the compiler won't let you [without any warning] pass a list of objects of one class and a comparator of a different class, so the comparison will always work.

like image 36
cd1 Avatar answered Mar 03 '26 00:03

cd1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!