Hello,
for the record, this is the Collator implementation I wrote.
Bye, Carsten
=================
public int compare( String source, String target ) {
// a tragical error in most code pages ß comes befor ä,ü,ö,
// but must be sorted after sz. Replace it for comparison
// by sz
source = source.replaceAll( "ß", "ss" );
target = target.replaceAll( "ß", "ss" );
// ä equals ae
source = source.replaceAll( "ä", "ae" );
source = source.replaceAll( "Ä", "Ae" );
target = target.replaceAll( "ä", "ae" );
target = target.replaceAll( "Ä", "Ae" );
// ö equals oe
source = source.replaceAll( "ö", "oe" );
source = source.replaceAll( "Ö", "Oe" );
target = target.replaceAll( "ö", "oe" );
target = target.replaceAll( "Ö", "Oe" );
// ü equals ue
source = source.replaceAll( "\u00fc", "ue" );
source = source.replaceAll( "\u00dc", "Ue" );
target = target.replaceAll( "\u00fc", "ue" );
target = target.replaceAll( "\u00dc", "Ue" );
if ( source.equals( target ) ) {
return 0;
}
int index = -1;
// compare char by char until the first difference occures
int ls = source.length( );
int lt = target.length( );
while ( true ) {
// reached end of one string ?
if ( ++index > ls ) {
return -10 - index;
}
if ( index > lt ) {
return 10 + index;
}
// common substring ?
if ( !( source.substring( 0, index ).equals( target.substring( 0, index ) ) ) ) {
break;
}
}
index--;
//look at the remaining difference
char sDiffChar = source.charAt( index );
char tDiffChar = target.charAt( index );
// both are letters, compare using unicode
if ( Character.isLetter( sDiffChar ) && Character.isLetter( tDiffChar ) ) {
return ( sDiffChar < tDiffChar ) ? ( -100 ) : 100;
}
// one is digit, one is letter, digit first
if ( Character.isLetterOrDigit( sDiffChar ) && Character.isLetterOrDigit( tDiffChar ) ) {
return Character.isDigit( sDiffChar ) ? ( -1000 ) : 1000;
}
// both are digit, try to find the longest possible integers
if ( Character.isDigit( sDiffChar ) && Character.isDigit( tDiffChar ) ) {
StringBuffer sb = new StringBuffer( );
sb.append( sDiffChar );
StringBuffer tb = new StringBuffer( );
tb.append( tDiffChar );
boolean foundDigit = true;
while ( foundDigit ) {
foundDigit = false;
if ( Character.isDigit( source.charAt( ++index ) ) ) {
sb.append( source.charAt( index ) );
foundDigit = true;
}
if ( Character.isDigit( target.charAt( index ) ) ) {
tb.append( target.charAt( index ) );
foundDigit = true;
}
}
int snumber = Integer.parseInt( sb.toString( ) );
int tnumber = Integer.parseInt( tb.toString( ) );
return ( snumber < tnumber ) ? ( -10000 ) : 10000;
}
return -10000;
}