com.fdsapi.arrays
Class ArrayComparator

java.lang.Object
  |
  +--com.fdsapi.arrays.ArrayComparator
All Implemented Interfaces:
java.util.Comparator

public class ArrayComparator
extends java.lang.Object
implements java.util.Comparator

This class allows you to sort Object[][] arrays by multiple columns. Each column can be either sorted in ascending or descending order. It does for Object[][] what the "order by " clause does for SQL select statements (i.e. select * from employees order by dept asc, lname asc, salary desc

The returned sort depends on the underlying Objects in the array. nulls are allowed and are returned last for both ascending and descending sorts.



View Code


Constructor Summary
ArrayComparator()
           
ArrayComparator(int sortCol, java.lang.String direction)
          Constructor to use when you know what column and direction you would like to sort on.
ArrayComparator(java.lang.String[] header)
          Creates a new instance filter and allows the columns to be referenced by the label in both display columns and where clauses.
 
Method Summary
 void addSortCol(int sortCol, java.lang.String direction)
          Used to specify what columns are to be sorted in what order (asc or desc).
 void addSortCol(java.lang.String sortColName, java.lang.String direction)
          Used to specify what columns are to be sorted in what order (asc or desc).
 int compare(java.lang.Object o1, java.lang.Object o2)
          Method used by the comparator interface.
static java.lang.Object[][] copy(java.lang.Object[][] data)
           
static void main(java.lang.String[] args)
          Method that has sample usage and test code
 void sort(java.lang.Object[][] array)
          Sorts the passed 2 dimensional array based on the data the already added sort columns.
static void sort(java.lang.Object[][] array, int sortIndex, java.lang.String sortDir)
          Sorts the passed 2 dimensional array based on the data in column sortIndex.
 java.lang.String toString()
          Return a string representation of the ordering that was selected.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Constructor Detail

ArrayComparator

public ArrayComparator()

ArrayComparator

public ArrayComparator(int sortCol,
                       java.lang.String direction)

Constructor to use when you know what column and direction you would like to sort on. Note other columns can be added to the sort column list after the object has been constructed.

Sample:

     // Sort the array (Object[][]) in ascending order based on column 0 
     //    (i.e. the first column of the array
     Arrays.sort(array, new ArrayComparator(0, "asc"));
 


ArrayComparator

public ArrayComparator(java.lang.String[] header)

Creates a new instance filter and allows the columns to be referenced by the label in both display columns and where clauses. Arrays don't have the concept of named columns and this array gives them this capability. For example in the following example after calling this constructor col0 can be referred to as fname, and col1 can be referred to as lname. The names are not case sensitive

Sample Call:

  String[] header={"fname","lname"};
  ArrayFilter f=new ArrayFilter(header); 
  f.addDisplayCol("fname");
  f.addDisplayCol("lname");
  f.addConditional("LNAme","=","souza");
  Object[][] data=f.filter(names);
 

Method Detail

sort

public static void sort(java.lang.Object[][] array,
                        int sortIndex,
                        java.lang.String sortDir)
Sorts the passed 2 dimensional array based on the data in column sortIndex. sortDir can be either "asc" or "desc" (ascending, descending respectively). column 1 within the array is represented by a sortIndex of 0.


sort

public void sort(java.lang.Object[][] array)

Sorts the passed 2 dimensional array based on the data the already added sort columns.

Sample Code:

   ArrayComparator ac=new ArrayComparator();
   ac.addSortCol(0,"asc"); // sort by column 0 in ascending order first
   ac.addSortCol(1,"desc"); // then by column 1 in descending order
   ac.addSortCol(2,"asc");  // then by column 2 in ascending order
   ac.sort(array);  // note ac can be used to sort as many arrays as needed after it has been created.
 
Monitor String: ArrayComparator.sort(Object[][])


addSortCol

public void addSortCol(int sortCol,
                       java.lang.String direction)

Used to specify what columns are to be sorted in what order (asc or desc). The order that addSortCol(...) is called determines what order the array will be sorted. See the example. value of sortCol equal to 0 means the first column. For "direction" valid values are "asc", "ascending", "desc", and "descending" in any case.

Sample Code:

   ArrayComparator ac=new ArrayComparator();
   ac.addSortCol(0,"asc"); // sort by column 0 in ascending order first
   ac.addSortCol(1,"desc"); // then by column 1 in descending order
   ac.addSortCol(2,"asc");  // then by column 2 in ascending order
   ac.sort(array);  // note ac can be used to sort as many arrays as needed after it has been created.
 


Monitor String: ArrayComparator.sort(Object[][])


addSortCol

public void addSortCol(java.lang.String sortColName,
                       java.lang.String direction)

Used to specify what columns are to be sorted in what order (asc or desc). The order that addSortCol(...) is called determines what order the array will be sorted. The value passed must match the header values past into the constructor and are translated to the index of the column header. For "direction" valid values are "asc", "ascending", "desc", and "descending" in any case.

Sample Code:

   String[] header={"fname", "lname", "salary"};
   ArrayComparator ac=new ArrayComparator(header);
   ac.addSortCol("lname","asc"); // sort by column 0 in ascending order first
   ac.addSortCol("fname","desc"); // then by column 1 in descending order
   ac.addSortCol(2,"asc");  // then by column 2 in ascending order
   ac.sort(array);  // note ac can be used to sort as many arrays as needed after it has been created.
 


Monitor String: ArrayComparator.sort(Object[][])


toString

public java.lang.String toString()
Return a string representation of the ordering that was selected. Examples are:
1) order by col1 desc, col2 asc
2) order by lname desc, fname asc, col3 asc

Overrides:
toString in class java.lang.Object

compare

public int compare(java.lang.Object o1,
                   java.lang.Object o2)
Method used by the comparator interface.

o1 < o2 - returns a negative integer
o1==o2 - returns zero
o1>o2 - returns a postitive integer

Iterate through all columns that should be compared (in the proper order) and call the Comparator for each of the column elements.

Specified by:
compare in interface java.util.Comparator

copy

public static java.lang.Object[][] copy(java.lang.Object[][] data)

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Method that has sample usage and test code

java.lang.Exception