001: package com.jamonapi.utils;
002:
003: /**
004:
005: * ArraySorter is used to sort 2 dimensional arrays of objects by one of the columns in the array. Right now this class
006:
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:
011: **/
012:
013: import java.util.*;
014:
015: class ArraySorter {
016:
017: private static void display(Object[][] array) {
018:
019: int rows = array.length;
020:
021: int cols = array[0].length;
022:
023: for (int i = 0; i < rows; i++) {
024:
025: String rowData = "";
026:
027: for (int j = 0; j < cols; j++) {
028:
029: rowData += array[i][j] + " ";
030:
031: }
032:
033: System.out.println(rowData);
034:
035: }
036:
037: }
038:
039: public ArraySorter(Object[][] array, int sortCol, String sortOrder) {
040:
041: this .array = array;
042:
043: this .sortCol = sortCol;
044:
045: this .sortOrder = sortOrder;
046:
047: }
048:
049: private Object[][] array;
050:
051: private int sortCol;
052:
053: private String sortOrder;
054:
055: private int getRows() {
056:
057: return array.length;
058:
059: }
060:
061: private int getCols() {
062:
063: return array[0].length;
064:
065: }
066:
067: private Object[] getArrayToSort() {
068:
069: Object[] arrayToSort = new Object[getRows()];
070:
071: for (int i = 0; i < getRows(); i++) {
072:
073: arrayToSort[i] = array[i];
074:
075: }
076:
077: return arrayToSort;
078:
079: }
080:
081: private ArraySorterEntry[] getArraySorterEntries() {
082:
083: Object[] arrayToSort = getArrayToSort();
084:
085: ArraySorterEntry[] arraySorterEntries = new ArraySorterEntry[getRows()];
086:
087: for (int i = 0; i < getRows(); i++) {
088:
089: arraySorterEntries[i] = new ArraySorterEntry(
090: arrayToSort[i], (Comparable) array[i][sortCol]);
091:
092: }
093:
094: return arraySorterEntries;
095:
096: }
097:
098: public Object[][] sort() {
099:
100: ArraySorterEntry[] arraySorterEntries = getArraySorterEntries();
101:
102: Arrays.sort(arraySorterEntries);
103:
104: Object[][] returnArray = new Object[getRows()][getCols()];
105:
106: for (int i = 0; i < getRows(); i++) {
107:
108: returnArray[i] = (Object[]) arraySorterEntries[i]
109: .getSortedObject();
110:
111: }
112:
113: return returnArray;
114:
115: }
116:
117: /**
118:
119: * inner class ArraySorterEntry
120:
121: **/
122:
123: private class ArraySorterEntry implements Comparable {
124:
125: private Object arrayValueToSort;
126:
127: private Comparable valueToSortBy;
128:
129: public ArraySorterEntry(Object arrayValueToSort,
130: Comparable valueToSortBy) {
131:
132: this .arrayValueToSort = arrayValueToSort;
133:
134: this .valueToSortBy = valueToSortBy;
135:
136: }
137:
138: public int compareTo(Object o) {
139:
140: ArraySorterEntry sorter = (ArraySorterEntry) o;
141:
142: int compare = valueToSortBy.compareTo(sorter.valueToSortBy);
143:
144: if (compare == 0 || "asc".equalsIgnoreCase(sortOrder))
145:
146: return compare;
147:
148: else if ("desc".equalsIgnoreCase(sortOrder))
149:
150: return -compare;
151:
152: else
153:
154: throw new RuntimeException(
155: "Programming error: The only valid sort orders are 'asc' and 'desc', but '"
156: + sortOrder + "' was passed");
157:
158: }
159:
160: public Object getSortedObject() {
161:
162: return arrayValueToSort;
163:
164: }
165:
166: }
167:
168: /******************* end inner class */
169:
170: /** Test code for ArraySorter **/
171:
172: public static void main(String[] args) throws Exception {
173:
174: Object[][] array = { { "7", "8", "9" }, { "1", "2", "3" },
175: { "4", "5", "6" }, };
176:
177: System.out.println("unsorted array");
178:
179: display(array);
180:
181: System.out.println("sorted array: asc on col 0");
182:
183: ArraySorter sorter = new ArraySorter(array, 0, "asc");
184:
185: display(sorter.sort());
186:
187: System.out.println("sorted array: desc on col 0");
188:
189: sorter = new ArraySorter(array, 0, "desc");
190:
191: display(sorter.sort());
192:
193: array = new Double[][] { { new Double(10), new Double(30) },
194: { new Double(20), new Double(40) } };
195:
196: System.out.println("sorted double array: desc on col 1");
197:
198: sorter = new ArraySorter(array, 1, "desc");
199:
200: display(sorter.sort());
201:
202: }
203:
204: }
|