Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Sorting Collections of Objects in Java

The examples that I wrote / finished yesterday ended up with a program called "Zoe" in Java, which read all the records in an access log file, created an object for each of them (which, sensibly would have been called a Hit but we called a "Zoe"), and reported on that object creation.

To finish off that example this morning, I sorted the objects (and I chose to do so based on the web server returned status codes, and where they were the same, the visiting host name), and printed them out. Th efollowing changes were made to the source code:

1. Set the class to implement "Comparable" which means that we have added a compareTo method which returns an integer (-ve, 0, +ve)

2. Called Collections.Sort on the ArrayList object (Collections.Sort is a static method of the Collection object type in java.util)

3. Provided a toString method so that we can easily display an Aileen (that's the new name of the class that I used as I wanted to leave Zoe available for delegates to look back at the intermediate stage) object.

4. Looped through our ArrayList after sorting in to output all the members in it.

In code terms:

public class Aileen implements Comparable {

Within the main program / test harness:

Collections.sort(Visits);
for (int k=0; k<Visits.size(); k++) {
  System.out.println(Visits.get(k));
  }


And new methods within the class:

public int compareTo(Object that) {
 // Can we differentiate by status?
 int newstate = this.status - ((Aileen)that).status;
 if (newstate != 0) return newstate;
 // If we can't, differentiate on host name
 return (this.Host.compareTo(((Aileen)that).Host));
 }
 
public String toString() {
 String rezult = Host + " " + /* Request + " " + */
   Time + " " + status;
 return rezult;
 }


You can sorted other ordered collection types in Java using the same mechanism, but you cannot sort any of the Hash types (Hash, HashMap, HashSet) - if you want their members in order, copy the keys to a Vector or an ArrayList.

You can also provide a Comparator object as a parameter to your call to Collections.Sort; be default, objects are sorted into what is know as their natural sort order by reference to the compareTo method, but if you provide Comparators you can have different ways of sorting them.

Full source code here.
(written 2009-09-25)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J714 - Java - Fundamental classes
  [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)
  [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)
  [1062] Java sorting - comparable v comparator - (2007-02-02)
  [42] Do languages change? - (2004-09-08)


Back to
Exceptions in Java - why and how
Previous and next
or
Horse's mouth home
Forward to
Looking inside Java classes - javap and javadoc
Some other Articles
Weekend and Christmas Promotion - Well House Manor Hotel, Melksham
A Winter Weekend Special at Well House Manor
What is a JVM, a JRE, a JDK - components of the core Java Environment
Looking inside Java classes - javap and javadoc
Sorting Collections of Objects in Java
Exceptions in Java - why and how
Where is my Java class?
Java Programming Fundamentals
Automating access to a page obscured behind a holding page
4344 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 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2421_Sor ... -Java.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat