SortWithCollationKeys.java Source code

Java tutorial

Introduction

Here is the source code for SortWithCollationKeys.java

Source

import java.text.CollationKey;
import java.text.Collator;
import java.util.Vector;

public class SortWithCollationKeys {
    public static void main(String[] args) {
        Vector<String> list = new Vector<String>();
        list.add("m");
        list.add("c");
        list.add("e");
        list.add("c");

        Collator collate = Collator.getInstance();

        CollationKey[] keys = new CollationKey[list.size()];

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

        CollationKey tmp;
        for (int i = 0; i < keys.length; i++) {
            for (int j = i + 1; j < keys.length; j++) {
                if (keys[i].compareTo(keys[j]) > 0) {
                    tmp = keys[i];
                    keys[i] = keys[j];
                    keys[j] = tmp;
                }
            }
        }
        for (int l = 0; l < keys.length; l++) {
            System.out.println(keys[l].getSourceString());
        }
    }
}