Adverts
I recently had to sort and subsort a number of objects for display and thought that this common task might make a good article. Normally I perform all my sorting using a database. It's generally quicker and simpler to just sort the records before you retrive them. Why is this the case: simple really, databases are optimized for data sorting and just like you wouldn't write your own XML parser you don't, generally, want to be writing your own sorting algorithums (at least not after you have left college). If you only have a few records or if it's difficult to sort the data in the database it can sometimes make sense to sort them yourself.
Most people can quickly figure out how to sort based on one field of their data but a surprisingly large number come unstuck when they need to sort based on two or more fields. This generally causes a question such as "How do I sort my table based on two columns" or "How do I subsort this list". It's actually not very much more complex to sort on n-fields as it is on one field. An example is shown below most of which is just set up. The important part is the nested if statements in the Comparator. Each nesting sorts on another field. With a little work it would be easy to generalize this to sort on any number of fields in any order.
|
package com.crazysquirrel.examples.sorting; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import com.crazysquirrel.examples.AbstractExample; public class Subsorting extends AbstractExample { @Override public void execute() { List<Person> people = new ArrayList<Person>(); people.add( new Person( "Bob", "Smith", 24 ) ); people.add( new Person( "Jane", "Grey", 18 ) ); people.add( new Person( "Sarah", "Green", 54 ) ); people.add( new Person( "Frank", "Jones", 32 ) ); people.add( new Person( "Jane", "Grey", 44 ) ); people.add( new Person( "John", "Grey", 44 ) ); printList( people ); log.warning( "Sorting: last name, first name, age" ); Collections.sort( people, new Comparator<Person>() { @Override public int compare( Person p1, Person p2 ) { int result = 0; if( ( result = p1.getLastName().compareTo( p2.getLastName() ) ) == 0 ) { if( ( result = p1.getFirstName().compareTo( p2.getFirstName() ) ) == 0 ) { result = p1.getAge() - p2.getAge(); } } return result; } }); printList( people ); } private void printList( List<Person> people ) { log.info( "People (in list order):" ); for( Person p : people ) { log.info( p.toString() ); } } private static class Person { private String firstName; private String lastName; private int age; public Person( String firstName, String lastName, int age ) { setFirstName( firstName ); setLastName( lastName ); setAge( age ); } public void setFirstName( String firstName ) { this.firstName = firstName; } public String getFirstName() { return firstName; } public void setLastName( String lastName ) { this.lastName = lastName; } public String getLastName() { return lastName; } public void setAge( int age ) { this.age = age; } public int getAge() { return age; } public String toString() { return getLastName() + " " + getFirstName() + " (" + getAge() + ")"; } } } |
| Java2html |