Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

collection.sort() explanation

 
vijay umar
Ranch Hand
Posts: 100
  • 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: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic