Skip to content

Instantly share code, notes, and snippets.

@KL-7
Created June 15, 2012 16:15
Show Gist options
  • Save KL-7/2937338 to your computer and use it in GitHub Desktop.
Save KL-7/2937338 to your computer and use it in GitHub Desktop.
ICU collation test
import com.ibm.icu.lang.UCharacter;
import com.ibm.icu.text.Collator;
public class CollationDemo {
private Collator collator;
public static void main(String arg[]) {
new CollationDemo().runDemo();
}
public void runDemo() {
collator = Collator.getInstance();
System.out.println(collationElement(UCharacter.toString(0x410) + UCharacter.toString(0x62)));
System.out.println(collationElement(UCharacter.toString(0x430) + UCharacter.toString(0x306) + UCharacter.toString(0x334)));
}
public String collationElement(String str) {
byte[] sortKey = collator.getCollationKey(str).toByteArray();
return byteArrayToString(sortKey);
}
public String collationElement(int cp) {
return collationElement(UCharacter.toString(cp));
}
public String byteArrayToString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
String delim = "";
for (int b : bytes) {
sb.append(delim).append(Integer.toHexString(b & 0xFF));
delim = ",";
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment