[ http://issues.apache.org/struts/browse/STR-1687?page=all ] David Evans reopened STR-1687: ------------------------------
Assign To: David Evans (was: Struts Developer Mailing List) > Sorting enhancements for LabelValueBean > --------------------------------------- > > Key: STR-1687 > URL: http://issues.apache.org/struts/browse/STR-1687 > Project: Struts Action 1 > Type: Improvement > Components: Action > Versions: Unknown > Environment: Operating System: All > Platform: All > Reporter: Paul Sundling > Assignee: David Evans > Priority: Minor > Fix For: 1.2 Family > Attachments: sortableBean.txt > > Hopefully you will see fit to accept my code and allow me to make my first > non-documentation open source contribution. The code I have added makes it > VERY > easy to sort an array of LabelValueBeans for display in select lists. > Here's a sample of how to use the newly added functionality > <inside the execute function of an Action used for setup> > LabelValueBean[] sortableList = new LabelValueBean[] { > new LabelValueBean("unorganized", "ung"), > new LabelValueBean("out of order", "ood"), > new LabelValueBean("Capitalized", "Cap"), > new LabelValueBean"("Not Lowercase", "Nl"), > }; > // to sort the list alphabetically by label > java.util.Arrays.sort(sortableList); > // or to sort the list case insensitive by label > java.util.Arrays.sort(sortableList, LabelValueBean.CASE_INSENSITIVE_ORDER); > request.setAttribute("sortableList", sortableList); > //then the sortableList is used in the <html:options> tags.. > Following is the output of the cvs diff: > # cvs diff -u > cvs server: Diffing . > Index: LabelValueBean.java > =================================================================== > RCS file: > /home/cvspublic/jakarta-struts/src/share/org/apache/struts/util/LabelValueBean.java,v > retrieving revision 1.6 > diff -u -r1.6 LabelValueBean.java > --- LabelValueBean.java 4 Jul 2003 18:26:19 -0000 1.6 > +++ LabelValueBean.java 15 Aug 2003 17:09:53 -0000 > @@ -62,7 +62,7 @@ > package org.apache.struts.util; > > import java.io.Serializable; > - > +import java.util.Comparator; > /** > * A simple JavaBean to represent label-value pairs. This is most commonly > used > * when constructing user interface elements which have a label to be > displayed@@ -72,9 +72,11 @@ > * @author Craig R. McClanahan > * @author Martin F N Cooper > * @author David Graham > + * @author Paul Sundling > + * > * @version $Revision: 1.6 $ $Date: 2003/07/04 18:26:19 $ > */ > -public class LabelValueBean implements Serializable { > +public class LabelValueBean implements Comparable, Serializable { > > > // ----------------------------------------------------------- > Constructors@@ -179,4 +181,31 @@ > public int hashCode() { > return (this.getValue() == null) ? 17 : this.getValue().hashCode(); > } > + /** > + * The comparable interface allows sorting. This is done based on the > + * label, since that is the human viewable part of the object. > + * @see java.lang.Comparable > + */ > + public int compareTo(Object otherBean) { > + // implicitly tests for the correct type, throwing ClassCastException > + // as required by interface > + String otherLabel = ((LabelValueBean)otherBean).getLabel(); > + > + //compare the labels of the LabelValueBean objects > + return this.getLabel().compareTo(otherLabel); > + } > + > + /** > + * Comparator object that can be used for a case insensitive order > + * sort of LabelValueBean objects. The idea for this comes from > + * java.lang.String > + */ > + public static Comparator CASE_INSENSITIVE_ORDER = new Comparator() { > + public int compare(Object Bean1, Object Bean2) { > + String label1 = ((LabelValueBean)Bean1).getLabel(); > + String label2 = ((LabelValueBean)Bean2).getLabel(); > + return label1.compareToIgnoreCase(label2); > + > + } > + }; > } -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/struts/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]