Sort string array with Collator : Collator « I18N « Java Tutorial






import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.Collator;

public class Main {
  public static void main(String args[]) throws Exception {
    String[] words = { "", "e", "a", "c" };
    Writer w = new BufferedWriter(new OutputStreamWriter(System.out, "Cp850"));
    for (int i = 0; i < 4; i++) {
      w.write(words[i] + " ");
    }
    sortArray(Collator.getInstance(), words);
    for (int i = 0; i < 4; i++) {
      w.write(words[i] + " ");
    }
    w.flush();
    w.close();
  }
  public static void sortArray(Collator collator, String[] strArray) {
    String tmp;
    if (strArray.length == 1)
      return;
    for (int i = 0; i < strArray.length; i++) {
      for (int j = i + 1; j < strArray.length; j++) {
        if (collator.compare(strArray[i], strArray[j]) > 0) {
          tmp = strArray[i];
          strArray[i] = strArray[j];
          strArray[j] = tmp;
        }
      }
    }
  }
}








13.22.Collator
13.22.1.Compare accentuated letters
13.22.2.Check Equality for two strings with Collator
13.22.3.Sort strings using Collator class
13.22.4.Sort string array with Collator