Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why is this arraylist sort not working?  RSS feed

 
Jim Henessey
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a simple app to keep track of my cds. Development has been going well, but I'm running into problems with an arraylist sort - it sorts the cds alphabetically that fall under a specific letter. The problem - my sorting methods, whether I implement them in a Comparator.compare or compareTo don't seem to be called.

Here's my Comparator compare method:



Method is called by the line: Collections.sort(letterSequence,new LetterCompare());

letterSequence is an arraylist - based on a check in another part of the program, all objects added to arraylist have a non-null album title field.
All other parts of the program are working properly.

I've tried putting similar code in a compareTo method in the AlbumData class, as well an inner class in the Collections input - it just never seems to be called. When I change the return to a straight "return 0", "return -1", "return 1" - the order is unaffected.

As a last measure I've tried putting System.out.println(...) in the compare method to check if it is even being called, but nothing is printed - whether that's the nature of the method or another sign that the method isn't called, I'm not sure.

This seems to be a very basic sort, close to all the examples I've seen - what am I doing wrong?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we see the method where you are calling this? It looks correct, and if you put a System.out.println() in there and it isn't being printed, then it isn't being called.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you sure the list is not empty ?
can you use a debugger and set a breakpoint inside the compare method and before the call to Collections.sort() ?

if the system.out.println() does not appear on the console, then your compare() method is not called.

pascal
 
Jim Henessey
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everyone, thanks for the help on this one. Pascal - thanks for focussing my mind on the ultimate problem. The list wasn't empty, but it was always containing only one item (calling line was in the wrong loop) - when list contains only one item, compare isn't even called.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!