001: package com.jamonapi.utils;
002:
003: import java.util.*;
004:
005: /**
006: * ArraySorter is used to sort 2 dimensional arrays of objects by one of the columns in the array. Right now this class
007: * sorts only for the monitor report, but could be made more generic. Look in the main method to see how this class is used.
008: *
009: **/
010: class ArraySorter {
011:
012: private static void display(Object[][] array) {
013:
014: int rows = array.length;
015: int cols = array[0].length;
016:
017: for (int i = 0; i < rows; i++) {
018: String rowData = "";
019: for (int j = 0; j < cols; j++) {
020: rowData += array[i][j] + " ";
021: }
022:
023: System.out.println(rowData);
024: }
025: }
026:
027: public ArraySorter(Object[][] array, int sortCol, String sortOrder) {
028: this .array = array;
029: this .sortCol = sortCol;
030: this .sortOrder = sortOrder;
031: }
032:
033: private Object[][] array;
034: private int sortCol;
035: private String sortOrder;
036:
037: private int getRows() {
038: return array.length;
039: }
040:
041: private int getCols() {
042: return array[0].length;
043: }
044:
045: private Object[] getArrayToSort() {
046:
047: Object[] arrayToSort = new Object[getRows()];
048: for (int i = 0; i < getRows(); i++) {
049: arrayToSort[i] = array[i];
050: }
051:
052: return arrayToSort;
053:
054: }
055:
056: private ArraySorterEntry[] getArraySorterEntries() {
057: Object[] arrayToSort = getArrayToSort();
058:
059: ArraySorterEntry[] arraySorterEntries = new ArraySorterEntry[getRows()];
060:
061: for (int i = 0; i < getRows(); i++) {
062: arraySorterEntries[i] = new ArraySorterEntry(
063: arrayToSort[i], (Comparable) array[i][sortCol]);
064: }
065:
066: return arraySorterEntries;
067: }
068:
069: public Object[][] sort() {
070:
071: ArraySorterEntry[] arraySorterEntries = getArraySorterEntries();
072: Arrays.sort(arraySorterEntries);
073:
074: Object[][] returnArray = new Object[getRows()][getCols()];
075: for (int i = 0; i < getRows(); i++) {
076: returnArray[i] = (Object[]) arraySorterEntries[i]
077: .getSortedObject();
078: }
079:
080: return returnArray;
081:
082: }
083:
084: /**
085: * inner class ArraySorterEntry
086: **/
087: private static final Float zero = new Float(0);
088:
089: private class ArraySorterEntry implements Comparable {
090: private Object arrayValueToSort;
091: private Comparable valueToSortBy;
092:
093: public ArraySorterEntry(Object arrayValueToSort,
094: Comparable valueToSortBy) {
095: this .arrayValueToSort = arrayValueToSort;
096: this .valueToSortBy = convert((String) valueToSortBy);
097: }
098:
099: // THIS IS UGLY. FIX THIS BY HAVING EACH COLUMN BE IN ITS NATIVE DATA TYPE NOT Strings.
100: // THIS CLASS IS SUPPOSED TO BE GENERIC BUT RIGHT NOW IT WILL ONLY WORK FOR SORTING THE MONITOR REPORT
101: private Comparable convert(String sortByStr) {
102: // 3,000 convert to 30000 and return as Float. Note a 0 replaces the comma, so values will be sorted in number
103: // order. The extra 0's for commas will not affect the sorting order adversely.
104: // without doing this 2, 1000, 1 would be sorted 1,1000, 2 instead of 1,2,1000
105:
106: // sortCol has a 0 based index where 0 is the monitor label first column.
107: if (sortCol >= 1 && sortCol <= 9)
108: return Float.valueOf(sortByStr.replace(',', '0'));
109: else if (sortCol >= 13) { // range format 3,000/22/4,555 returns 3000 or   if there are no /
110: int index = sortByStr.indexOf("/");
111: if (index == -1)
112: return zero;
113: else
114: return Float.valueOf(sortByStr.substring(0, index)
115: .replace(',', '0'));
116: } else
117: return sortByStr;
118:
119: }
120:
121: public int compareTo(Object o) {
122: ArraySorterEntry sorter = (ArraySorterEntry) o;
123: int compare = valueToSortBy.compareTo(sorter.valueToSortBy);
124: if (compare == 0 || "asc".equalsIgnoreCase(sortOrder))
125: return compare;
126: else if ("desc".equalsIgnoreCase(sortOrder))
127: return -compare;
128: else
129: throw new RuntimeException(
130: "Programming error: The only valid sort orders are 'asc' and 'desc', but '"
131: + sortOrder + "' was passed");
132: }
133:
134: public Object getSortedObject() {
135: return arrayValueToSort;
136: }
137:
138: }
139:
140: /******************** end inner class */
141:
142: /** Test code for ArraySorter **/
143:
144: public static void main(String[] args) throws Exception {
145: Object[][] array = { { "7", "8", "9" }, { "1", "2", "3" },
146: { "4", "5", "6" }, };
147: System.out.println("unsorted array");
148: display(array);
149:
150: System.out.println("sorted array: asc on col 0");
151: ArraySorter sorter = new ArraySorter(array, 0, "asc");
152: display(sorter.sort());
153:
154: System.out.println("sorted array: desc on col 0");
155: sorter = new ArraySorter(array, 0, "desc");
156: display(sorter.sort());
157:
158: System.out
159: .println("sorted array invalid - RuntimeException will be thrown");
160: sorter = new ArraySorter(array, 0, "invalid");
161: display(sorter.sort());
162:
163: }
164: }
|