Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 2738 invoked from network); 17 Mar 2006 08:12:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 17 Mar 2006 08:12:45 -0000 Received: (qmail 80665 invoked by uid 500); 17 Mar 2006 08:12:44 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 80302 invoked by uid 500); 17 Mar 2006 08:12:42 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 80291 invoked by uid 99); 17 Mar 2006 08:12:42 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Mar 2006 00:12:42 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [192.87.106.226] (HELO ajax.apache.org) (192.87.106.226) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Mar 2006 00:12:41 -0800 Received: by ajax.apache.org (Postfix, from userid 65534) id D49D2D49FF; Fri, 17 Mar 2006 08:12:20 +0000 (GMT) From: bugzilla@apache.org To: commons-dev@jakarta.apache.org Subject: DO NOT REPLY [Bug 39008] New: - Add a multi property beancomparator Message-ID: X-Bugzilla-Reason: AssignedTo Date: Fri, 17 Mar 2006 08:12:20 +0000 (GMT) X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG� RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT . ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND� INSERTED IN THE BUG DATABASE. http://issues.apache.org/bugzilla/show_bug.cgi?id=39008 Summary: Add a multi property beancomparator Product: Commons Version: unspecified Platform: All OS/Version: Windows XP Status: NEW Severity: enhancement Priority: P3 Component: Collections AssignedTo: commons-dev@jakarta.apache.org ReportedBy: mathus.b@mipih.fr Hi, I recently needed to be able to sort a list of beans on many properties. So I thought I would try and pick one of the collections comparators. I had to to the sorting close to the sql way : be able to sort on n properties, some ascending, some descending. I haven't found what I'm looking for (:p), but I found some comparators in the commons I used to do this : I used BeanComparator, NullComparator and ComparatorChain to create a class : MultiPropertyBeanComparator. Is there already something in one of the commons package that could be used instead of it ? If not, I'd be glad to contribute the small piece of code if wanted. It has dependencies against commons-beanutils (BeanComparator, which is moving from one package to another at the moment, no ?) and commons-lang (StringUtils.isBlank()). I think some things might not satisfactory for everybody, but hey, could still be improved without problems, that's not big work :p. As adviced on the user mailing list, I'm posting the code on this bugzilla, so as maybe one of the coder could take a look at it. Here it is : ======================================= import java.io.Serializable; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.collections.ComparatorUtils; import org.apache.commons.collections.comparators.NullComparator; import org.apache.commons.collections.comparators.ReverseComparator; import org.apache.commons.lang.StringUtils; /** * This comparator lets you sort a list using a list of properties. You can specify ascending or * descending order on these properties. *

* For example, if you want to sort with natural order a list of beans with properties firstname, * nickname and address, sorting the firstname descending, you can do it this way: *

* List l = ... * ... * MultiPropertyBeanComparator multiPropBeanComp = new MultiPropertyBeanComparator(); * multiPropBeanComp.append("firstname", true).append("nickname").append("address"); * Collections.sort(l,multiPropBeanComp); * * @author Baptiste MATHUS */ public class MultiPropertyBeanComparator implements Comparator, Serializable { private static final long serialVersionUID = -1431852774261001458L; private List comparatorList = new ArrayList(); /** * Use this method to add a comparator to the list. * * @param property * the property on which to apply the given comparator. * @param comparator * the comparator to be added. If null, natural order will be used. * @param reverse *

* must be true if the given comparator must be used in opposite order to sort. For * example, if the comparator is designed to sort in ascending order, put this * parameter to true if you want descending order. *

*

* If the comparator is null, then the reversed natural order is used. *

*/ public MultiPropertyBeanComparator append(String property, Comparator comparator, boolean reverse) { if (StringUtils.isBlank(property)) { throw new IllegalArgumentException("The given property is blank"); } // If the comparator is null, then compare only on the given property // with a natural sort. if (comparator == null) { comparator = new BeanComparator(property, new NullComparator(false)); } // Else : compare on the property, but with given comparator. else { comparator = new BeanComparator(property, comparator); } // Here, the comparator cannot be null anymore, so reverse it if // necessary. if (reverse) { comparator = new ReverseComparator(comparator); } comparatorList.add(comparator); return this; } public MultiPropertyBeanComparator append(String property, Comparator c) { return append(property, c, false); } public MultiPropertyBeanComparator append(String property) { return append(property, null, false); } public MultiPropertyBeanComparator append(String property, boolean reverse) { return append(property, null, reverse); } /** * Use this method to clear the */ public void clear() { comparatorList.clear(); } /** * Considered to be equal when all properties and comparators equal. * * @see java.lang.Object#equals(java.lang.Object) * @overrides */ public boolean equals(Object obj) { MultiPropertyBeanComparator comp = (MultiPropertyBeanComparator) obj; if (this.comparatorList.size() != comp.comparatorList.size()) { return false; } for (int i = 0; i < comparatorList.size(); ++i) { if (!this.comparatorList.get(i).equals(comp.comparatorList.get(i))) { return false; } } return true; } /** * @see Comparator#compare(T, T) * @overrides */ public int compare(Object arg0, Object arg1) { return getComparator().compare(arg0, arg1); } private Comparator getComparator() { return ComparatorUtils.chainedComparator(comparatorList); } } ============= -- Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are the assignee for the bug, or are watching the assignee. --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org