Java List Sort sortStringEnumeration(Enumeration list)

Here you can find the source of sortStringEnumeration(Enumeration list)

Description

Sort the given String enumeration.

License

Open Source License

Parameter

Parameter Description
list List of strings

Return

A sorted vector of String.

Declaration

public static Vector sortStringEnumeration(Enumeration list) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    /**//from  w  w w . jav a2 s. co m
     * Sort the given String enumeration.
     *
     * @param  list  List of strings
     * @return A sorted vector of String.
     */
    public static Vector sortStringEnumeration(Enumeration list) {
        Vector sorted = new Vector();
        while (list.hasMoreElements())
            orderedStringInsert((String) list.nextElement(), sorted);
        sorted.trimToSize();
        return sorted;
    }

    /**
     * Insert a String into a vector, maintaing the order.
     *
     * @param key The string to insert.
     * @param into The target sorted vector.
     */
    static void orderedStringInsert(String key, Vector into) {
        int lo = 0;
        int hi = into.size() - 1;
        int idx = -1;
        String item = null;
        int cmp = 0;

        if (hi >= lo) {
            while ((hi - lo) > 1) {
                idx = (hi - lo) / 2 + lo;
                item = (String) into.elementAt(idx);
                cmp = item.compareToIgnoreCase(key);
                if (cmp == 0) {
                    return;
                } else if (cmp < 0) {
                    lo = idx;
                } else if (cmp > 0) {
                    hi = idx;
                }
            }
            switch (hi - lo) {
            case 0:
                item = (String) into.elementAt(hi);
                if (item.equals(key))
                    return;
                idx = (item.compareToIgnoreCase(key) < 0) ? hi + 1 : hi;
                break;
            case 1:
                String loitem = (String) into.elementAt(lo);
                String hiitem = (String) into.elementAt(hi);
                if (loitem.equals(key))
                    return;
                if (hiitem.equals(key))
                    return;
                if (key.compareToIgnoreCase(loitem) < 0) {
                    idx = lo;
                } else if (key.compareToIgnoreCase(hiitem) < 0) {
                    idx = hi;
                } else {
                    idx = hi + 1;
                }
                break;
            default:
                throw new RuntimeException("implementation bug.");
            }
        }
        // Add this key to the vector:
        if (idx < 0)
            idx = 0;
        into.insertElementAt(key, idx);
        return;
    }
}

Related

  1. sortNames(List nameLs, String splitStr)
  2. sortRepositorynames(List list)
  3. sortSimpleName(List list)
  4. sortSimpleName(List list)
  5. sortString(Set lists)
  6. sortStringList(List l, boolean ascending)
  7. sortStringList(List stringList)
  8. sortStringList(String[] listToSort, String[] priorityList)
  9. sortUniq(List list)