FileDocCategorySizeDatePackage
SortWithCollationKeys.javaAPI DocExample1244Fri Mar 30 00:14:56 BST 2001None

SortWithCollationKeys

public class SortWithCollationKeys extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

    // Build a vector of words to be sorted
    Vector list = new Vector();
    list.add("mocha");
    list.add("chocolate");
    list.add("espresso");
    list.add("cappuccino");

    // Obtain a collator
    Collator collate = Collator.getInstance();

    // Create an array to hold the CollationKey objects
    CollationKey[] keys = new CollationKey[list.size()];

    // Fill the array
    for (int k = 0; k < list.size(); k ++)
      keys[k] = collate.getCollationKey((String)list.elementAt(k));

    // Use a standard bubble sort to collate the keys
    CollationKey tmp;
    for (int i = 0; i < keys.length; i++) {
      for (int j = i + 1; j < keys.length; j++) {
        // Compare the keys
        if ( keys[i].compareTo( keys[j] ) > 0 ) {
          // Swap keys[i] and keys[j]
          tmp = keys[i];
          keys[i] = keys[j];
          keys[j] = tmp;
        }
      }
    }

    // Display the sorted results
    StringBuffer result = new StringBuffer();
    for (int l= 0;l < keys.length; l++) {
      System.out.println(keys[l].getSourceString());
    }