Do Comparators Work In Android?

Since the Comparator class is part of the Java programming language, Android should do too. However, my experience with using them seem to indicate that it may not work all the time. Even I think it does not work in Android.

Has anyone else experienced this? Or even used it and worked? I made a Comparator class and ran it in Java’s runtime environment. It worked correctly. When I used the same class and sorted an ArrayList object in the Android emulator, it did not work. The sorting was different.

Now, I have no idea why. I always thought that there was something wrong with my custom Comparator class because the output in the Android emulator was always wrong. When I ran it in the JRE (since it was faster), the output was correct.

There should not be any rules to using comparators in the Android emulator. How they are used should be the same as when used in the Java Runtime Environment.

Please share your thoughts. Your inputs are highly appreciated and may help me know the limitations and what ifs when using a Comparator class in an Android emulator.

Related Posts Plugin for WordPress, Blogger...

2 comments

  1. That’s really interesting. As I understand you have problems not with Comparator itself, but wiith Collections.sort() method. As I can see in the Android sources Google uses different sorting algorithm then Sun in its JDK (TimSort vs. quick sort). Maybe there is a hidden bug in the former implementation.
    It would be great if you could post some code which could reproduce the issue. (Or if you’d like you can post a question on StackOverflow and left a link here). Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *