List of usage examples for org.apache.commons.collections4 ComparatorUtils reversedComparator
public static <E> Comparator<E> reversedComparator(final Comparator<E> comparator)
From source file:com.caocao.util.CollectionUtils.java
/** * ??sortTheList ??List?/*from w w w . j a v a2s .c o m*/ * * @param list * @param sortFields * @param sortOrder */ public static void sortTheList(List list, String[] sortFields, String sortOrder) { if (sortFields == null || sortFields.length <= 0) { return; } ArrayList sorts = new ArrayList(); Comparator c = ComparatorUtils.NATURAL_COMPARATOR; c = ComparatorUtils.nullLowComparator(c); // ?null if (StringUtils.isEquals(sortOrder, CollectionUtils.SORT_ORDER_DESC)) { c = ComparatorUtils.reversedComparator(c); // ? } String sortField = null; for (int i = 0; i < sortFields.length; i++) { sortField = sortFields[i]; if (StringUtils.isNotEmpty(sortField)) { sorts.add(new BeanComparator(sortField, c)); } } ComparatorChain multiSort = new ComparatorChain(sorts); Collections.sort(list, multiSort); }
From source file:de.alpharogroup.collections.ListExtensions.java
/** * Sort over the given property. Note: the property should be implement the Comparable * interface.//from ww w . ja va2s. c o m * * @param <T> * the generic type of the list * @param list * the list to sort. * @param property * the property to sort. * @param ascending * if true the sort will be ascending ohterwise descending. */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T> void sortByProperty(final List<T> list, final String property, final boolean ascending) { Comparator comparator = new BeanComparator(property, new ComparableComparator()); if (ascending) { comparator = ComparatorUtils.reversedComparator(comparator); } Collections.sort(list, comparator); }
From source file:org.openvpms.web.component.im.query.PageLocator.java
/** * Adds a key to sort on./*from w w w . j a v a 2 s . com*/ * <p/> * This: * <ul><li>adds a select constraint for the specified node</li> * <li>adds a sort constraint</li> * <li>registers a comparator to locate the object within pages</li> * </ul> * <p/> * Note that all queries will be sorted on ascending "id" by default. * * @param node the node name * @param ascending determines if the node is sorted in ascending or descending order * @param comparator the comparator. If {@code ascending} is {@code false}, it will be reversed */ public <T extends Comparable> void addKey(String node, boolean ascending, Comparator<T> comparator) { query.add(new NodeSelectConstraint(node)); query.add(new NodeSortConstraint(node, ascending)); if (!ascending) { comparator = ComparatorUtils.reversedComparator(comparator); } comparators.addComparator(new NodeComparator<T>("a." + node, comparator)); keySet.set("a." + node, bean.getValue(node)); }