Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How does the sort() method in Java work?  RSS feed

 
Ranch Hand
Posts: 153
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
Bear with me! A newbie is only trying to get a visual cue as to what is going on behind the scene.


Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface.

My sources of confusion:

1.  The sort() method has not been overridden. So, at what point is the compareTo() method invoked?
2.  On which element in the list is the compareTo() method invoked; that is (e1.name).compareTo(e2.name).  How are e1 and e2 chosen from the list?
3.  How does the sort() method iterate through the list in order to sort it?
 
Master Rancher
Posts: 3001
105
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Biniman,

1) The Collections class is a subclass of Object. Since Object does not have a 'sort' method, the 'sort' method of Collections can't be an override. The 'compareTo' is invoked any time the sort method wants to compare two elements of the list

2) As said: the compareTo method is invoked any time when two elements must be compared. So, if the elements e1 and e2 are involved, 'sort' invokes 'e1.compareTo(e2)' (and since a decent 'compareTo' method requires that 'e1.compareTo(e2)' is equal to 'e2.compareTo(e1), it doesn't matter if e1. or e2. is being used).

3) The API of 'Collections.sort' does not specify which particular sort algorithm is used. What elements and how many are involved in the sorting, is depending on that algorithm used.

Look at Wiki for some well-known sorting algorithms, and try to implement one of the simpler sort algorithms, using a List of, say, Integers or Strings (these implement the Comparable interface). See what elements you use, and when you invoke the 'compareTo' method.
 
Biniman Idugboe
Ranch Hand
Posts: 153
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I grab hold of this part of your reply

'sort' invokes 'e1.compareTo(e2)'

Did I hear somebody still saying Java is easy to learn?
 
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may be wrong by I remember reading somewhere that for small lists (ones where you do not need to think of BigO) Insertion sort is used while for larger ones quicksort or mergesort.  
 
Marshal
Posts: 61727
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Philip Freeman wrote:. . .  Insertion sort is used while for larger ones quicksort or mergesort.  

You shou‍ld look at the documentation, for example for List#sort(), because it tells you which algorithm it uses. Arrays#sort() may be different.
 
Bartender
Posts: 1856
81
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Biniman Idugboe wrote:Did I hear somebody still saying Java is easy to learn?


Easy can be a relative term, which can be hard to define as it can change from person to person.

That being said, there are some things in Java which can be done rather quickly, like writing "Hello World" to the screen.
And then there are other things which can be rather difficult to learn like using some of the classes in the collections library.
Then there is also the fact that not everyone has the same level of experience when they start learning something new like Java and not everyone learns the same way.

The big thing here is, 'Do not give up'.
Learning something like Java can be a daunting task to undertake, and even if you do not end up using this skill you will still be the better for learning it.
Learning is the best way to keep your mind active and delay some of the illnesses associated with old age.
 
Biniman Idugboe
Ranch Hand
Posts: 153
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hear you, loud and clear!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!