-
Notifications
You must be signed in to change notification settings - Fork 3
/
ColumnComparator.java
210 lines (177 loc) · 4.98 KB
/
ColumnComparator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import java.util.Comparator;
import java.util.List;
/**
* Sort an object based on a specified column within the object.
* The object must be either a List or an Array. Several sort
* properties can be set:
*
* a) ascending (default true)
* b) ignore case (default true)
* c) nulls last (default true)
*
* An array of primitives can also be sorted. Only the ascending
* property is considered for the sort.
*
* Use the Arrays.sort() method with the ColumnComparator to sort Arrays.
* All rows in the Array must be of the same class, but they can be either:
* - an Array
* - a List
*
* Use the Collections.sort(.) method with the ColumnComparator to sort Lists.
* All rows in the List must be of the same class, but they can be either:
* - a List
* - an Array
*/
public class ColumnComparator implements Comparator
{
private int column;
private boolean isAscending;
private boolean isIgnoreCase;
private boolean isNullsLast = true;
/*
* The specified column will be sorted using default sort properties.
*/
ColumnComparator(int column)
{
this(column, true);
}
/*
* The specified column wil be sorted in the specified order
* with the remaining default properties
*/
ColumnComparator(int column, boolean isAscending)
{
this(column, isAscending, true);
}
/*
* The specified column wil be sorted in the specified order and
* case sensitivity with the remaining default properties
*/
ColumnComparator(int column, boolean isAscending, boolean isIgnoreCase)
{
setColumn( column );
setAscending( isAscending );
setIgnoreCase( isIgnoreCase );
}
/*
* Set the column to be sorted
*/
public void setColumn(int column)
{
this.column = column;
}
/*
* Set the sort order
*/
public void setAscending(boolean isAscending)
{
this.isAscending = isAscending;
}
/*
* Set whether case should be ignored when sorting Strings
*/
public void setIgnoreCase(boolean isIgnoreCase)
{
this.isIgnoreCase = isIgnoreCase;
}
/*
* Set nulls position in the sort order
*/
public void setNullsLast(boolean isNullsLast)
{
this.isNullsLast = isNullsLast;
}
/*
* Implement the Comparable interface
*/
public int compare(Object a, Object b)
{
// Sorting of primitives/objects is done separately
String className = a.getClass().getName();
if (className.length() == 2)
{
return primitiveSort(a, b, className);
}
else
return objectSort(a, b);
}
private int primitiveSort(Object a, Object b, String className)
{
int result = 0;
if (className.equals("[Z"))
result = Boolean.compare(((boolean[])a)[column], ((boolean[])b)[column]);
else if (className.equals("[B"))
result = Byte.compare(((byte[])a)[column], ((byte[])b)[column]);
else if (className.equals("[C"))
result = Character.compare(((char[])a)[column], ((char[])b)[column]);
else if (className.equals("[D"))
result = Double.compare(((double[])a)[column], ((double[])b)[column]);
else if (className.equals("[F"))
result = Float.compare(((float[])a)[column], ((float[])b)[column]);
else if (className.equals("[I"))
result = Integer.compare(((int[])a)[column], ((int[])b)[column]);
else if (className.equals("[I"))
result = Integer.compare(((int[])a)[column], ((int[])b)[column]);
else if (className.equals("[J"))
result = Long.compare(((long[])a)[column], ((long[])b)[column]);
else if (className.equals("[S"))
result = Short.compare(((short[])a)[column], ((short[])b)[column]);
if (! isAscending)
result *= -1;
return result;
}
@SuppressWarnings("unchecked")
private int objectSort(Object a, Object b)
{
Object o1 = null;
Object o2 = null;
// The object to be sorted must be a List or an Array
if (a instanceof List)
{
List list1 = (List)a;
List list2 = (List)b;
o1 = list1.get(column);
o2 = list2.get(column);
}
if (a.getClass().isArray())
{
Object[] a1 = (Object[])a;
Object[] a2 = (Object[])b;
o1 = a1[column];
o2 = a2[column];
}
// Treat empty strings like nulls
if (o1 instanceof String && ((String)o1).length() == 0)
{
o1 = null;
}
if (o2 instanceof String && ((String)o2).length() == 0)
{
o2 = null;
}
// Handle sorting of null values
if (o1 == null && o2 == null) return 0;
if (o1 == null) return isNullsLast ? 1 : -1;
if (o2 == null) return isNullsLast ? -1 : 1;
// Compare objects
int result = 0;
if (o1 instanceof Comparable)
{
if (o1 instanceof String
&& isIgnoreCase)
result = ((String)o1).compareToIgnoreCase((String)o2);
else
result = ((Comparable)o1).compareTo(o2);
}
else // Compare as a String
{
if (isIgnoreCase)
result = o1.toString().compareToIgnoreCase(o2.toString());
else
result = o1.toString().compareTo(o2.toString());
}
if (! isAscending)
result *= -1;
return result;
}
}