Using your example of comparing Strings and Double, let's look at String
method:
That is not what I said. The columns must have COMPATIBLE types in
them. e.g. all Strings, all Doubles, though different columns might
have different types.
I just don't want a big case clause to sort a column depending on the
type that is currently in it. It is a reversion to C.
Here some code from CSVSort that stinks.
/**
* Sort on arbitrary list of columns.
* Defines default the sort order for SortableCSVRow Objects.
* Compare this SortableCSVRow with another SortableCSVRow
with JDK 1.5+ generics.
* Compares key then key2 then key3.
* Informally, returns (this-other) or +ve if this is more
positive than other.
*
* @param other other SortableCSVRow to compare with this one
*
* @return +ve if this>other, 0 if this==other, -ve if
this<other
*/
public final int compareTo( SortableCSVRow other )
{
int diff;
// sort over keys till find a non-tie.
for ( int i = 0; i < CSVSort.this.countOfColsToSort; i++ )
{
switch ( CSVSort.this.sortTypes[ i ] )
{
case 's': // case sensitive, default compare
String
if ( CSVSort.this.isAscendings[ i ] )
{
/* ascending */
diff = ( ( String ) this.keys[ i ]
).compareTo( ( String ) other.keys[ i ] );
}
else
{
/* descending */
diff = ( ( String ) other.keys[ i ]
).compareTo( ( String ) other.keys[ i ] );
}
break;
case 'i':
// case insensitive
if ( CSVSort.this.isAscendings[ i ] )
{
/* ascending */
diff = ( ( String ) this.keys[ i ]
).compareToIgnoreCase( ( String ) other.keys[ i ] );
}
else
{
/* descending */
diff = ( ( String ) other.keys[ i ]
).compareToIgnoreCase( ( String ) this.keys[ i ] );
}
break;
case 'n': // represented as Double
if ( CSVSort.this.isAscendings[ i ] )
{
/* ascending */
diff = ( ( Double ) this.keys[ i ]
).compareTo( ( Double ) other.keys[ i ] );
}
else
{
/* descending */
diff = ( ( Double ) other.keys[ i ]
).compareTo( ( Double ) this.keys[ i ] );
}
break;
case 'x': // represented as Long
if ( CSVSort.this.isAscendings[ i ] )
{
/* ascending */
diff = ( ( Long ) this.keys[ i ]
).compareTo( ( Long ) other.keys[ i ] );
}
else
{
/* descending */
diff = ( ( Long ) this.keys[ i ]
).compareTo( ( Long ) other.keys[ i ] );
}
break;
default:
throw new IllegalArgumentException( "Program
bug: invalid SortType: " + sortTypes[ i ] );
}
if ( diff != 0 )
{
return diff;
}
}
return 0;
}