|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object | +--com.fdsapi.arrays.ArrayComparator
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.
| 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 |
public 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"));
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 |
public static void sort(java.lang.Object[][] array,
int sortIndex,
java.lang.String sortDir)
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[][])
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.
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.
public java.lang.String toString()
toString in class java.lang.Object
public int compare(java.lang.Object o1,
java.lang.Object o2)
compare in interface java.util.Comparatorpublic static java.lang.Object[][] copy(java.lang.Object[][] data)
public static void main(java.lang.String[] args)
throws java.lang.Exception
java.lang.Exception
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||