Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Java sorting - comparable v comparator

In Java, you can sort objects that implement the Collection interface, using either the comparable interface or a comparator class. What's the difference?

A Comparable interfaced class must contain a method called compareTo to compare two objects (one being the object on which it is called and the other being passed as a paramater) which returns an integer, negative for a < b, positive for a > b and 0 otherwise. It must also contain an equals method, returning a boolean, on the same parameters.

A Comparator is a class in its own right, which implements the Comparator interface; that means it must contain a method called compare (two objects as parameters) which returns a negative, zero or positive integer depensing on whether the first object is less than, equal to, or greater than the second.

If you want to sort a collection using its comparable interface, you simply call the static Collections.sort method on it ... so if we had an ArrayList called Actable which implemented Comparable, we could write:
Collections.sort(Actable);

To sort a collection using a Comparator class, you need to pass an extra parameter into the Collections.sortmethod - that parameter being an instance of a Comparator object. Thus:
Collections.sort(Actable, new Bynum());

So in effect, using the comparable interface gives you a default sort order of your own making, whereas the comparator is more flexible as it allows you define a whole series of different types of sorting that can be applied to a whole lot of different types of objects.
(written 2007-02-02, updated 2007-02-05)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J714 - Java - Fundamental classes
  [4431] A Java servlet that is also a stand alone program. And a server that is also a web client. - (2015-02-19)
  [4421] How healthy are the stars of stage and screen? - (2015-02-09)
  [4396] Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. - (2015-01-16)
  [4330] Java - factory method, encapsulation, hashmap example - (2014-11-27)
  [3048] String handling - from first steps to practical examples - (2010-11-13)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2734] for and foreach in Java - (2010-04-22)
  [2649] Length, size or capacity in Java? - (2010-02-24)
  [2421] Sorting Collections of Objects in Java - (2009-09-25)
  [2418] Viv.java uses unchecked or unsafe operations - explanation and cure - (2009-09-24)
  [2323] Java Collection Objects in the java.util package - (2009-08-05)
  [1910] Java - Generics - (2008-11-27)
  [1502] Java, sorting, ArrayList example, generics - (2008-01-11)
  [42] Do languages change? - (2004-09-08)


Back to
Take vehicles off the road - put all the passengers into one
Previous and next
or
Horse's mouth home
Forward to
Bradford-on-Avon - 150 years of passenger trains
Some other Articles
Final, Finally and Finalize - three special words in Java
Graham Ellis - an Introduction
Light and shadows at Beach
Bradford-on-Avon - 150 years of passenger trains
Java sorting - comparable v comparator
Take vehicles off the road - put all the passengers into one
What a relief from the tax office.
Three sets of twins
PHP Regular expression to extrtact link and text
Selling by phone and Skype - our policy
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

1 unpublished comment pending on this page

edit your own (not yet published) comments

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1062_Jav ... rator.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat