0

I have a problem doing this since I need to sort it in this way:

Before:

{aAbB, abBA, AaBb}

After:

{AaBb, aAbB, abBA}

The idea is sorting a uppercase letter right before it's lowercase version, and that with every letter. I'm actually using a Collator for Spanish (problems with the 'ñ') and set it's strength to PRIMARY so I can compare if two words are equal not having care of capital letters.

Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30

2 Answers2

0

Just do:

private static Comparator<String> ALPHABETICAL_ORDER = new Comparator<String>() {
    public int compare(String str1, String str2) {
        int res = String.CASE_INSENSITIVE_ORDER.compare(str1, str2);
        if (res == 0) {
            res = (str1.compareTo(str2)==0) ? 1 : 0;
        }
        return res;
    }
};

Collections.sort(list, ALPHABETICAL_ORDER);

Inspired from: Simple way to sort strings in the (case sensitive) alphabetical order

Community
  • 1
  • 1
Cukic0d
  • 5,111
  • 2
  • 19
  • 48
0

This produces the desired result.

Arrays.sort(stringArray) 
prashant
  • 348
  • 3
  • 13
  • While this code may answer the question, it would be better to explain how it solves the problem without introducing others and why to use it. Code-only answers are not useful in the long run. – JNYRanger Dec 01 '15 at 19:48