• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing interface Comparable and its method CompareTo  RSS feed

 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New to java and trying to understand the Comparable interface and its associated method CompareTo(which i have generated using netbeans). I have this code below which have the pings of planets recorded over a time.







I would like to sort the objects in my arraylist called "theSounds" in descending order by their lengths. I know i have to use Collections.sort(theSounds) but not sure how to get this to word in the compareTo method.
I have looked at the documentation https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html but still cant get my head round this.

Could someone explain how I can go about this please?
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like you have the wrong class implementing Comparable.
Since you want to sort a Collection of Sounds then it's the Sounds class that should be Comparable.
 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, thats what I thought initially as well but because I have my arraylist in the SortSounds method, thought I could sort them in descending order from this class?
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wendy Jackson wrote:yes, thats what I thought initially as well but because I have my arraylist in the SortSounds method, thought I could sort them in descending order from this class?


You will be sorting them in that class.
The sort() method uses the compareTo method of a Comparable to determine the ordering.
Write compareTo on Sounds so it orders "normally", and use the reverseOrder() method to get a reversed comparator.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wendy Jackson wrote:. . . Comparable . . . method CompareTo . . .
It is not called CompareTo. Similarly your Sounds class shou‍ld be called Sound. I also think you shou‍ld call the field something different.
I see you have seen the Java™ Tutorials section (well done looking there first ) and if you cannot work out how to compare by length of words, I think you need to resort to desperate measures. Find a pencil and paper and eraser and turn your computer off. When you can write down how to distinguish the lengths of Strings on paper, then you will find it easy enough to write in code.

Since lengths of words are always positive, the comparisons can be simpler than in some circumstances.
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compareTo should be about comparing two instances based on some natural ordering. I am not sure that ordering on the basis of the length of the strings is such a natural way. Therefore I suggest to start with some Comparators first. These are easy to define and apply, and after seeing some results it might help to identify a candidate for the 'natural' ordering. If 'list' is a List, then we have the method 'list.sort(comparator) to sort the list according to the supplied comparator.

To make the code slightly less verbose, I added the method 'getSoundsLength() to the Sounds class. Here goes:

and
 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the clear explanation. I should be able to build up on this.
Cheerss
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!