aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes collection.sort() explanation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 16687
    
  19

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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: collection.sort() explanation
 
Similar Threads
java.lang.Set dupplicate problem
Building a class which initializes abject like String class?
How to call a generic method with two bounds (for a single argument) if one bound needs a cast
JSF annotations on interface or abstract class
Alternative to com.google.common.base.Joiner ?