Sorting Vector Element using I18N

This Example shows you how to sort vector element using I18N. In the code given below we are sorting some elements using some rules.

Sorting Vector Element using I18N

Sorting Vector Element using I18N

     

This Example shows you how to sort vector element using I18N. In the code given below we are sorting some elements using some rules.

Methods used in this example are described below :

Vector.add() : Vector is like an array, it contains elements that can be accessed using an integer index. However, the size of a Vector can increase or decrease as needed to adjust adding and removing items after the Vector has been created. add() method is used to appends the Fixed element to the end of this Vector.

Collator.getInstance() : The Collator class prosecutes locale-sensitive String comparison. You use this class to build searching and sorting routines for natural language text. getInstance() method is used to create a instance of Collator class.

SortString.java
import java.text.*;
import java.util.*;

public class SortString {
 public static void main(String[] args) {
  Vector v = new Vector();
  v.add("komal");
  v.add("Komal");
  v.add("Lalu");
  v.add("lalu");
  v.add("santosh");
  v.add("rakesh");
  v.add("Chandu");
  v.add("Ajay");
  v.add("chandu");

 Collator esCollator = Collator.getInstance(new Locale("es""ES"));
 String spanishRules = ((RuleBasedCollatoresCollator).getRules();

 try {
 String rules = "& C < ch, cH, Ch, CH " "& L < ll, lL, Ll, LL";
 RuleBasedCollator collate = new RuleBasedCollator(spanishRules + rules);
 Collections.sort(v, collate);
  catch (Exception e) {
 System.out.println(e);
 }
  System.out.println("Sorted Name ...");
 Enumeration result = v.elements();
 while (result.hasMoreElements()) {
 System.out.println(result.nextElement().toString());
 }
  }
}

Output :


Sorted Name ...
Ajay
chandu
Chandu
komal
Komal
lalu
Lalu
rakesh
santosh

Download code