Bug 3146

Summary: input:select does not preserve order
Product: Taglibs Reporter: Paul Hettl <paul.hettl>
Component: String TaglibAssignee: Tomcat Developers Mailing List <dev>
Status: CLOSED WONTFIX    
Severity: enhancement    
Priority: P3    
Version: 1.0   
Target Milestone: ---   
Hardware: PC   
OS: Linux   

Description Paul Hettl 2001-08-16 06:57:25 UTC
When creating a selection list with the input taglib the order of elements gets 
mixtes up. this is a problem in case of e.g. Month selection list.

my workaround is: use two ArrayList object for values and lables:

input.tld change: 

<attribute>
	<name>optionLabels</name>
	<required>true</required>
	<rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
	<name>optionValues</name>
	<required>true</required>
	<rtexprvalue>true</rtexprvalue>
    </attribute>


code change:

    private ArrayList optionLabels ;        // the lables
    private ArrayList optionValues ;        // the submitted values
    public void setOptionValues(ArrayList x) {
        optionValues = x;
    }
    public void setOptionLabels(ArrayList x) {
        optionLabels = x;
    }

 in doStart():

 // changed:
            //
            if ( optionLabels.size() != optionValues.size() ){
                throw new JspException(
                            "option keys and option values must be of equal 
size");
            }
            HashMap options = new HashMap();
            ListIterator lIter = optionLabels.listIterator();
            int j = 0;

            while ( lIter.hasNext() ) {
                Object  key = (Object) lIter.next();
                Object  value = (Object)  optionValues.get( j);
                j++;
                options.put( key, value);

            }

            // actually print the <option> tags
            if (options.size() > 0 ) {
            //if (options != null) {
                Iterator i = optionLabels.iterator();
Comment 1 Shawn Bayern 2001-08-16 07:22:46 UTC
Order isn't intended to be preserved; you can use a TreeMap if you want an
ordering against a Comparable, and you can also write a Map using a back-end
Vector if you want to preserve order of entry.  The key is to simplify the page
author's usage; using two separate Lists won't necessarily do that.

Thanks for the suggestion.  Do you have another suggested mechanism that won't
complicate usage?