File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

collection.sort() explanation

 
vijay umar
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vijay umar
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic