JDK-4348571 : Bug in class Collator with swedish locale
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_98
  • CPU: x86
  • Submitted: 2000-06-27
  • Updated: 2004-03-16
  • Resolved: 2004-03-16
Related Reports
Duplicate :  
Description

Name: rlT66838			Date: 06/27/2000


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


I have problem with class Collator when I use it with swedish locale. This code
will show the problem.

import java.util.*;
import java.text.*;
import java.io.*;

class Person implements Comparable, Serializable {

	private String code;
	private String name;
 	
	public Person(String code, String name) {
		this.code = code;
		this.name = name;
	}
	
	public int compareTo(Object o) {
		Person anotherPerson = (Person) o;
		Collator col = Collator.getInstance(new Locale("sv", "SE"));
		return col.compare(this.code, anotherPerson.code);
	}
	
	public String toString() {
		return code + "\t" + name;
	}
}

class TestSort
{
	public TestSort() {
		List users = new LinkedList();
		users.add(new Person("aa", "Johan"));
		users.add(new Person("bb", "Nils"));
		users.add(new Person("cc", "Anna"));
		users.add(new Person("cc", "Linda"));
		users.add(new Person("aq", "Viktor"));
		
		Collections.sort(users);
		showUsers(users);
	}
	
	private void showUsers(List users) {
		Iterator it = users.iterator();
		System.out.println("code\tname");
		while (it.hasNext()) {
			System.out.println(it.next());
		}
		System.out.println("");
	}
	
	public static void main(String[] args) {
		new TestSort();
	}
}

Start the application with java TestSort. The output will be

code    name
aq      Viktor
bb      Nils
cc      Anna
cc      Linda
aa      Johan

The problem is that the line with code aa should come first. It works fine if i
use Locale.US.

It also works fine when I use Linux with java -version
Classic VM (build 1.2.2, -L, green threads, nojit)

(Review ID: 106570) 
======================================================================

Comments
EVALUATION From Leif.Samuelsson@eng 2000-06-27: That looks like a bug, indeed. The collating for the letters a-z should be the same as for English. (Swedish does have some "umlauts" [\u00e4, \u00e5, \u00f6] that are sorted at the end of the alphabet, but this example doesn't contain any of those.) Here is a simplified test case that shows the bug: import java.text.Collator; import java.util.Locale; public class TestSort { public static void main(String[] args) { Collator col = Collator.getInstance(new Locale("sv", "SE")); System.out.println("compare(a, b) = " + col.compare("a", "b") + " (should be -1)"); System.out.println("compare(b, c) = " + col.compare("b", "c") + " (should be -1)"); System.out.println("compare(aa, bb) = " + col.compare("aa", "bb") + " (should be -1)"); System.out.println("compare(bb, cc) = " + col.compare("bb", "cc") + " (should be -1)"); } } This has been fixed in Tiger-b42 by the L10N team as 4804273. ###@###.### 2004-03-16
16-03-2004