Jiew Meng
Jiew Meng

Reputation: 88197

Make Java sort symbols after alphabet (like Linux sort does)

When I do a sort in Linux:

echo -en "[a\na" > test && sort test > test2

I get

a
[a

But in Java [a comes before a. Is it possible to make it work like Linux does?

Upvotes: 1

Views: 467

Answers (1)

Florent Bayle
Florent Bayle

Reputation: 11910

So, first thing, the sort order of the sort command depends on the locale (especially LC_COLLATE). With LC_ALL set to C, the sort order will be the same as Java:

$ echo -ne "[a\na" | LC_ALL=C sort
[a
a
$

So, if you just use the normal Java sort and put the letters and digits first, you can use something like that:

final List<String> lst = new ArrayList<String>();
lst.add("[a");
lst.add("a");

Collections.sort(lst, new Comparator<String>() {
    @Override
    public int compare(final String s1, final String s2) {
        if (s1==s2)
            return 0;
        if (s1==null)
            return 1;
        if (s2==null)
            return -1;
        for (int i=0; i<s1.length()&&i<s2.length(); i++)
            if (Character.isLetterOrDigit(s1.charAt(i)) | Character.isLetterOrDigit(s2.charAt(i))) {
                return Character.isLetterOrDigit(s1.charAt(i))?-1:1;
            } else if (s1.charAt(i)!=s2.charAt(i))
                break;
        return s1.compareTo(s2);
    }
});

for (final String str: lst)
    System.out.println(str);

But if you want something more complex, based on LC_COLLATE, you will have to use a Collator and maybe define your own RuleBasedCollator.

Upvotes: 1

Related Questions