DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT <http://nagoya.apache.org/bugzilla/show_bug.cgi?id=22467>. ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=22467 [PATCH] sorting enhancements for org.apache.struts.util.LabelValueBean Summary: [PATCH] sorting enhancements for org.apache.struts.util.LabelValueBean Product: Struts Version: Unknown Platform: All OS/Version: All Status: NEW Severity: Enhancement Priority: Other Component: Utilities AssignedTo: [EMAIL PROTECTED] ReportedBy: [EMAIL PROTECTED] 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); + + } + }; } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
