2

I've been attempting to sort text in French, but been having problems.

Java Collator class seems to order some strings incorrectly.

Please suggest why this is happening?

import java.util.List;
import java.util.stream.Stream;
import java.util.stream.Collectors;
import java.text.Collator;
import java.util.Locale;

public class HelloWorld{

     public static void main(String []args){
        String s1 = "un f";
        String s2 = "une b";
        Collator c = Collator.getInstance(Locale.FRANCE);
        List<String> arr1 = Stream.of(s1, s2).sorted(c).collect(Collectors.toList());
        System.out.println(arr1);
        // outputs  [une b, un f]
        // expected [un f, une b]
     }
}
Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
Henco
  • 59
  • 4
  • 1
    What is the output ? – Arnaud Jun 21 '18 at 13:45
  • [I get the same result when I use `Locale.ENGLISH`](https://tio.run/##pVBNT8IwGL7vVzQ7dVGb4FHUhCygJJODHI2H0pVZLC1p3xEN4bfPd@vGRxA52Eub93n7fC34mt8s8s@qUsuVdUAWOGAlKM0y5aEfnYw9OMmXbNpc5/HUai0FWOePd0B@QQNyxH75nlnBtexH0aqcaSWI0Nx78sKV2UQRqU8LeOCA19qqnCwRpuhImYK8vXNX@GQTlvG0c98jDyQuDZnH/RPsNmCSzA7A3aOzS@ZOGvGR4nI3YoWEsUEvRkgavLPR62CSDpM9Ud3kfZB6bDkGziFLKJHZOfW9a3SRMI9tyJy2QgkToUa6r5OBrflociAw/fYgkacEtkIV0IbGo4bijsRXO8Xkr2zSFFr5i@GGk6dsPH0@l65luRCv0/pPvmHgqAPuRbs/22hbVT8), although I agree I would have expected `un f` before `une b` (space before 'e'). – Kevin Cruijssen Jun 21 '18 at 13:54
  • 4
    Collator ignores spaces. https://stackoverflow.com/questions/16567287/java-collation-ignores-space. so you are comparing 'unf' with 'une' – Michal Jun 21 '18 at 13:55
  • 2
    It seems this has been a [jdk-bug](https://bugs.openjdk.java.net/browse/JDK-4546576) for a while. – Edwin Dalorzo Jun 21 '18 at 14:17

0 Answers0