Collator collator = Collator.getInstance(new Locale("en", "US"));For a detailed description and extra information refer to Strings - Core Java Technologies Technical Tips.
int comparison = collator.compare("cat", "Hat");
if (comparison < 0) {
System.out.printf("%s < %s\n", "cat", "Hat");
} else {
System.out.printf("%s < %s\n", "Hat", "cat" );
}
Wednesday, September 13, 2006
java.text.Collator for String Comparison
The String class doesn't have the ability to compare text from a natural language perspective. Its equals and compareTo methods compare the individual char values in the string. If the char value at index n in name1 is the same as the char value at index n in name2 for all n in both strings, the equals method returns true. The java.text.Collator class provides natural language comparisons. Natural language comparisons depend upon locale-specific rules that determine the equality and ordering of characters in a particular writing system.A Collator object understands that people expect "cat" to come before "Hat" in a dictionary. Using a collator comparison, the following code prints cat < Hat.
Labels:
example/sample code,
tips
Subscribe to:
Post Comments (Atom)
Popular Posts
-
This post will describe how to create and deploy a Java Web Application war to Heroku using Heroku CLI. You will need a basic understanding ...
-
In this post we will see how to do an offline install Jenkins and required plugins on a Red Hat Enterprise Linux Server release 7.3. This is...
-
Update: A new post for validation in struts with annotation is available at: Struts 2 Validation: Annotations . Struts 2.0 relies on a val...
-
Google has opensourced it's Google Web Toolkit . The project is now fully opensource under the Apache 2.0 license . The new 1.3 RC has...
-
The previous post described the Strategy pattern in brief. I listed out where and why the strategy pattern may be used. This post describes...
-
In a previous post, I described how to use Quartz scheduler for scheduling . In this post, I describe the configuration changes required for...
-
Java 8 introduced default and static methods, the previous post gives a few examples of Java 8 default and static methods . Java 9 builds on...
-
Tivoli Access Manager Configuration Steps for using AznAPI: 1.) Configure AMRTE 2.) Configure AMJRTE (This will install the necessary librar...
-
The previous post described the Command pattern in brief. I listed out where and why the command pattern may be used. This post describes h...
-
Just moved to blogger beta. Testing the changes.
is tere any sorting tool that uses java.text.collator
ReplyDeleteIs there any way to sort a list in the Case-sensitive order using Collation Keys ?
ReplyDeleteeg., I want an unsorted list say "A,b,B,c,a,C" to be sorted case-sensitively as "A,B,C,a,b,c".
Please provide your suggestions?