my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes collection.sort() explanation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "collection.sort() explanation" Watch "collection.sort() explanation" New topic
Author

collection.sort() explanation

vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
i have a doubt regarding usage of Collections.sort() method;

as far as i read from k & b book that he collections.sort() can only be used by the class which extends the collections class!

so we can not use it with user defined type like --- List<myClass> abc= new ArrayList<myClass>();

so here we cant apply like collections.sort(abc)? am i right? or there is one more method collections.sort(abc,comparaor c);
when does this method works? odes it works? if yes? how does it?

can we sort a userdefined list using Colllections.sort(); method any of the 2 above ways of declaration? if possible give an better example code? thank you!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

as far as i read from k & b book that he collections.sort() can only be used by the class which extends the collections class!

so we can not use it with user defined type like --- List<myClass> abc= new ArrayList<myClass>();



Collections sort() method can be used on any collections, provided that the contained data is Comparable. If the data is not Comparable, then you need to call the overloaded version, of the method, that takes a Comparator.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
thanks
 
wood burning stoves
 
subject: collection.sort() explanation