aspose file tools*
The moose likes Beginning Java and the fly likes About Comparable and Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Comparable and Comparator" Watch "About Comparable and Comparator" New topic
Author

About Comparable and Comparator

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi Folks,

The two interfaces are used for object sorting. well, i want to know they follow which sorting mechanism(algorithm). i guess insertion sorting. please can anyone give light on this ?
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Link to Collections class API. Look up the sort() methods there.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

i am not mentioned about which order(natural order or ascending order) they are sorted. i am about sorting technique.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

They are just interfaces, which allows to determine the natural order of the compared objects. The comparing algorithm is in the class doing the actual comparision. For example, java.util.Collections.sort(). The API says : The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2log(n) performance that would result from attempting to sort a linked list in place.


[My Blog]
All roads lead to JavaRanch
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Well Chrishtope, but merge sort needs two list[or array] , then it will sort and merge those two into one list. but here i have only one list which has the objects need to be sort. in this case you mean Collections class (sort()) use the merge sort?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

but here i have only one list which has the objects need to be sort.

Collections.sort() uses only one list, the list you want to sort.

in this case you mean Collections class (sort()) use the merge sort?

Yes.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks Christophe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Comparable and Comparator