Java Jobs
Java Papers
Servlet
Servlet Interview Questions
RoseIndia's Java
Sort Java (for sql query generated / except hibernate relational data) Collections
Full Example to sort Collection
Generic Sorts
If you write a sort for a List, e.g. ArrayList, with proper generics, it will work on collections of any type that supports Comparable or Comparator. To see how to pull it off, have a look at the source for any of my sorts, or Sun’s sort.However, because of Java’s lack of orthogonality, your List sort won’t work for arrays of such Objects. You need to write very similar code to do that. Even that array version won’t sort an array of primitives such as long, int or byte. You have to write yet another slightly different version of the sort to handle each type of primitive.
Complete Example:
package com.mindprod.example;
import com.mindprod.palette.NamedColor.Alphabetically;
import com.mindprod.palette.NamedColor.ByRGB;
import java.awt.Color;
import static java.lang.System.out;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
* Demonstrate how to sort a Collection in Various orders using Sun's Collections.sort
* Created by IntelliJ IDEA.
*
* @author Roedy Green
*/
public class TestSort
{
// --------------------------- main() method ---------------------------
public static void main( String[] args )
{
// build ArrayList of Animals
ArrayList<Animal> animals = new ArrayList<Animal>( 5 );
animals.add( new Animal( "panther", Color.BLACK, 4 ) );
animals.add( new Animal( "frog", Color.GREEN, 4 ) );
animals.add( new Animal( "crab", Color.RED, 10 ) );
animals.add( new Animal( "elephant", Color.GRAY, 2 ) );
animals.add( new Animal( "spider", Color.BLACK, 8 ) );
out.println( "sort in Natural Order (by legs)" );
Collections.sort( animals );
for ( Animal animal : animals )
{
out.println( animal.toString() );
}
out.println( "sort in Reverse Natural Order (by legs)" );
Collections.sort( animals, Collections.reverseOrder() );
for ( Animal animal : animals )
{
out.println( animal.toString() );
}
out.println( "sort alphabetically" );
Collections.sort( animals, new Animal.Alphabetically() );
for ( Animal animal : animals )
{
out.println( animal.toString() );
}
out.println( "sort reverse alphabetically" );
Collections.sort( animals, Collections.reverseOrder( new Animal.Alphabetically() ) );
for ( Animal animal : animals )
{
out.println( animal.toString() );
}
out.println( "sort by color" );
Collections.sort( animals, new Animal.ByRGB() );
for ( Animal animal : animals )
{
out.println( animal.toString() );
}
// end main
}
}
/**
* objects we will sort, represent animals, not nested
*/
class Animal implements Comparable<Animal>
{
/**
* what color this animal is.
*/
final Color color;
/**
* name of the animal
*/
final String name;
/**
* how many legs this animal has
*/
final int legs;
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* compare by legs then by colour (numerically).
*
* @param o other animal to compare with
* @return +ve if this animal is bigger, -ve if smaller, 0 if equal
*/
public int compareTo( Animal o )
{
int diff = legs - o.legs;
if ( diff != 0 )
{
return diff;
}
return ( color.getRGB() & 0xffffff ) - ( o.color.getRGB() & 0xffffff );
}
/**
* what colour is the animal
*
* @return colour
*/
public Color getColor()
{
return color;
}
/**
* how many legs has this animal?
*
* @return number of legs
*/
public int getLegs()
{
return legs;
}
/**
* override usual ToString to give a debugging represenation.
*
* @return name, color, legs as a string.
*/
public String toString()
{
return name + " " + color + " " + legs;
}
// --------------------------- CONSTRUCTORS ---------------------------
/**
* constructor
*
* @param name name of the animal.
* @param color what colonr the animal is
* @param legs howw many legs the animal has
*/
Animal( String name, Color color, int legs )
{
this.name = name;
this.color = color;
this.legs = legs;
}
/**
* Comparator for sorting Animals by alphabetically, case insensitive
*/
static class Alphabetically implements Comparator<Animal>
{
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* compare two animal objects
*
* @param o1 first object
* @param o2 second object
* @return +ve if o1 is biggern, -ve if smaller, 0 if equal
*/
public int compare( Animal o1, Animal o2 )
{
return o1.name.compareToIgnoreCase( o2.name );
}
}
/**
* Comparator for sorting Animals by Color then legs
*/
static class ByRGB implements Comparator<Animal>
{
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* compare two animal objects
*
* @param o1 first object
* @param o2 second object
* @return +ve if o1 is biggern, -ve if smaller, 0 if equal
*/
public int compare( Animal o1, Animal o2 )
{
int diff = ( o1.color.getRGB() & 0xffffff ) - ( o2.color.getRGB() & 0xffffff );
if ( diff != 0 )
{
return diff;
}
return o1.legs - o2.legs;
}
}
}