• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Why didn't my sorting work using Collections.sort()?  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I tried to sort on basis of their length.
Code compiles and runs but I don't get a sorted list.
Any Help is appreciated.
Thank you to all those who take the time to read and help me out.
 
Saloon Keeper
Posts: 5391
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the javadocs of the Comparable.compareTo method - under certain circumstances it returns a negative number, which your method never does.
 
Marshal
Posts: 13441
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Re-evaluate what your compareTo() method does. Check all possible cases (There are only three cases)
 
Karla Carr
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Check the javadocs of the Comparable.compareTo method - under certain circumstances it returns a negative number, which your method never does.


Thank you for helping me out. It worked now!
 
Marshal
Posts: 64089
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through the Java™ Tutorials. Under “interfaces”,, you will find a section about ordering. I think it is really good.
Also reread the documentation about Comparable. Do teams really have a total ordering by length of name? Maybe they can be ordered by name (alphabetically) or by number of games played, or by number of games/points won. In which case, with different ways to sort teams, a Comparator would be more appropriate. You will probably find all you need to know about Comparators in the same Java™ Tutorials section.
 
Sheriff
Posts: 21716
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're interested, Stuart Marks had a talk a year ago on Devoxx about this:

 
Campbell Ritchie
Marshal
Posts: 64089
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I liked those talks; it is always good to listen to somebody who really knows what he is talking about
 
Master Rancher
Posts: 3177
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't seen the video's. But I was wondering why nothing at all seems to be sorted. I tried this:

and I would have expected to see at least one swap, since "222".length() >  "33".length(). But it doesn't happen.

Now, as the video no doubt makes clear, the biggest problem with your Comparator is that it is not symmetric, i.e. compareTo(x, y) != compareTo(y, x) and not reflexive (is compareTo(x, x) == 0?)
For instance, compareTo("222", "33") gives 1, since 3 is obviously bigger than 2, but compareTo("33", "222") returns 0, meaning they are equal. So the result of this comparison is in what order Java compares the two, and I do not know that. It is therefore impossible to predict what the result of the sort will be. I put some print statements into your code, to see what elements get compared in what order. See if the output explains the result.
Thus: at least make sure that you compareTo or the Comparator that you use obeys the three laws: reflexive, symmetric and transitive.


 
Rob Spoor
Sheriff
Posts: 21716
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Now, as the video no doubt makes clear, the biggest problem with your Comparator is that it is not symmetric, i.e. compareTo(x, y) != compareTo(y, x))


Unlike with the equals method, the asymmetry is good, and should always be the case. It's often the case that compareTo(x, y) == -comparaTo(y, x). Although that is not a requirement, it is a requirement that if compareTo(x, y) > 0, then compareTo(y, x) < 0.

Thus: at least make sure that you compareTo or the Comparator that you use obeys the three laws: reflexive, symmetric and transitive.


Those are the laws for equals. The rules for comparing are very similar: reflexive (an object is never smaller or larger than itself), anti-symmetric (if x is larger than y, then y is smaller than x) and transitive (if x is larger than y and y is larger than z, then x is larger than z).
 
Just the other day, I was thinking ... about this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!