Sort strings using Collator class : Collator « I18N « Java






Sort strings using Collator class

 
 
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class Main {
  public static void main(String[] args) {
    List<String> fruits = new ArrayList<String>();
    fruits.add("A");
    fruits.add("");
    fruits.add("C");
    fruits.add("D");
    fruits.add("A");

    Collator collator = Collator.getInstance(Locale.US);

    Collections.sort(fruits, collator);

    for (int i = 0; i < fruits.size(); i++) {
      String fruit = fruits.get(i);

      System.out.println("Fruit = " + fruit);
    }
  }
}

   
  








Related examples in the same category

1.Collator.getInstance(new Locale("sv", ""))
2.Comparing Half-Width and full-width A
3.Collator.NO_DECOMPOSITION
4.Collator.CANONICAL_DECOMPOSITION
5.Collator.FULL_DECOMPOSITION
6.Sort Collate
7.I18N SortI18N Sort
8.Searching, Sorting, and Text Boundary Detection: Collation IssuesSearching, Sorting, and Text Boundary Detection: Collation Issues
9.Compare accentuated letters
10.Check Equality for two strings with Collator
11.I18N : TextI18N : Text