Alpha numeric sorting of rows in a JTable.
Hi All,
I am using the following class for performing alpha numeric sorting of the rows in my JTable.
import java.util.Comparator;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
/*
* The following class takes in a vector and performs alpha numeric sorting on it
*/
publicclass StepAlphanumComparatorimplements Comparator{
char[] numbers ={'1','2','3','4','5','6','7','8','9','0'};
privateint colIndex;
public StepAlphanumComparator(int colIndex){
this.colIndex = colIndex;
}
privateboolean isIn(char ch,char[] chars){
for (int i = 0; i < chars.length; i++){
if (ch == chars[i])
returntrue;
}
returnfalse;
}
privateboolean inChunk(char ch, String s){
if (s.length() == 0)
returntrue;
char s0 = s.charAt(0);
int chunkType = 0;// 0 = alphabetic, 1 = numeric
if (isIn(s0, numbers))
chunkType = 1;
if ((chunkType == 0) && (isIn(ch, numbers)))
returnfalse;
if ((chunkType == 1) && (!isIn(ch, numbers)))
returnfalse;
returntrue;
}
publicint compare(Object object1, Object object2){
Vector v1 = (Vector)object1;
Vector v2 = (Vector)object2;
Object column1 = v1.get(colIndex);
Object column2 = v2.get(colIndex);
String s1 = (String) column1;
String s2 = (String) column2;
int thisMarker = 0;
int thisNumericChunk = 0;
String thisChunk =new String();
int thatMarker = 0;
int thatNumericChunk = 0;
String thatChunk =new String();
while ((thisMarker < s1.length()) && (thatMarker < s2.length())){
char thisCh = s1.charAt(thisMarker);
char thatCh = s2.charAt(thatMarker);
thisChunk ="";
thatChunk ="";
while ((thisMarker < s1.length()) && inChunk(thisCh, thisChunk)){
thisChunk = thisChunk + thisCh;
thisMarker++;
if (thisMarker < s1.length())
thisCh = s1.charAt(thisMarker);
}
while ((thatMarker < s2.length()) && inChunk(thatCh, thatChunk)){
thatChunk = thatChunk + thatCh;
thatMarker++;
if (thatMarker < s2.length())
thatCh = s2.charAt(thatMarker);
}
int thisChunkType = isIn(thisChunk.charAt(0), numbers) ? 1 : 0;
int thatChunkType = isIn(thatChunk.charAt(0), numbers) ? 1 : 0;
// If both chunks contain numeric characters, sort them numerically
int result = 0;
if ((thisChunkType == 1) && (thatChunkType == 1)){
thisNumericChunk = Integer.parseInt(thisChunk);
thatNumericChunk = Integer.parseInt(thatChunk);
if (thisNumericChunk < thatNumericChunk)
result = -1;
if (thisNumericChunk > thatNumericChunk)
result = 1;
}else{
result = thisChunk.compareTo(thatChunk);
}
if (result != 0)
return result;
}
return 0;
}
}
This is how I use this class...
DefaultTableModel tableModel =new DefaultTableModel();
/* Populate the table ....
....
.....
*/
Vector data = tableModel.getDataVector();
Collections.sort(data,new StepAlphanumComparator(colIndex));
tableModel.fireTableStructureChanged();
Now my problem is when I am using the above class for an Abstract Table Model, it doesn't work, can anyone explain why. If you can give a sample code for making it work for an abstract table model, it would be a bonus.
Thanks,
Knight.

