Search code examples
javacomparatorebcdic

Java comparator for String in EBCDIC encoding


I have come across a requirement where I need to convert a string to EBCDIC encoding and then sort it. We need to sort it with EBCDIC because the string has to go in mainframe. The string I will sort will have only alphabets in captial and integers only.

I googled it some and then I came across the link from IBM which has listed the characters in order

What I realized was that EBCDIC sorting is exactly opposite to normal java lexicographic sorting (at least for the type of data which I am going to process).

My question is my realization right ? If not what I am missing ? OR is there any java comparator available for EBCDIC encoding.


Solution

  • Since the char type is implicitly UTF-16 in Java EBCDIC strings need to be compared as Java byte arrays.

    Example:

        Charset encoding = Charset.forName("IBM1047");
        Comparator<String> encComparator = (s1, s2) ->
                encoding.encode(s1)
                        .compareTo(encoding.encode(s2));