org.apache.commons.collections4.comparators.ReverseComparator Java Examples

The following examples show how to use org.apache.commons.collections4.comparators.ReverseComparator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DynamicValueSortedTreeMapTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testExplicitOrdered() {
	DynamicValueSortedTreeMap<String, Integer> queue =
		new DynamicValueSortedTreeMap<>(new ReverseComparator<>());
	queue.put("2nd", 2);
	queue.put("1st", 1);
	queue.put("3rd", 3);
	List<String> ordered = new ArrayList<>(queue.keySet());
	assertEquals(Arrays.asList(new String[] { "3rd", "2nd", "1st" }), ordered);
}
 
Example #2
Source File: BucketDefinition.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Comparator<Object> createOrderComparator(Comparator<Object> comparator, BucketOrder order)
{
	Comparator<Object> orderComparator;
	switch (order)
	{
		case DESCENDING:
		{
			if (comparator == null)
			{
				orderComparator = new ReverseComparator<Object>();
			}
			else
			{
				orderComparator = new ReverseComparator<Object>(comparator);
			}
			break;
		}
		case ASCENDING:				
		{
			if (comparator == null)
			{
				orderComparator = ComparableComparator.INSTANCE;
			}
			else
			{
				orderComparator = comparator;
			}
			break;
		}
		case NONE:
		default:
			throw 
				new JRRuntimeException(
					EXCEPTION_MESSAGE_KEY_UNSUPPORTED_ORDER_TYPE,
					new Object[]{order});
	}
	return orderComparator;
}
 
Example #3
Source File: SortUtil.java    From feilong-core with Apache License 2.0 3 votes vote down vote up
/**
 * 按照key desc 倒序排序.
 * 
 * <h3>注意:</h3>
 * <blockquote>
 * <ol>
 * <li><span style="color:red">原 <code>map</code> 的顺序不变</span></li>
 * <li>该方法使用了 {@link PropertyComparator},允许 null key,<b>null key排在最后面</b></li>
 * </ol>
 * </blockquote>
 * 
 * <h3>示例:</h3>
 * <blockquote>
 * 
 * <pre class="code">
 * Map{@code <String, Comparable>} map = new HashMap{@code <>}();
 * 
 * map.put("a", 123);
 * map.put("c", 345);
 * map.put(null, 88);
 * map.put("b", 8);
 * 
 * LOGGER.debug(JsonUtil.format(SortUtil.sortMapByKeyDesc(map)));
 * </pre>
 * 
 * <b>返回:</b>
 * 
 * <pre class="code">
 * {
 * "c": 345,
 * "b": 8,
 * "a": 123,
 * null: 88
 * }
 * </pre>
 * 
 * </blockquote>
 *
 * @param <K>
 *            the key type
 * @param <V>
 *            the value type
 * @param map
 *            the map
 * @return 如果 <code>map</code> 是null,返回 {@link Collections#emptyMap()}<br>
 * @see ReverseComparator#ReverseComparator(Comparator)
 * @see PropertyComparator#PropertyComparator(String)
 * @see #sortMap(Map, Comparator)
 * @since 1.8.0 move from MapUtil
 * @since 1.8.7 change method name
 */
public static <K, V> Map<K, V> sortMapByKeyDesc(Map<K, V> map){
    if (null == map){
        return emptyMap();
    }
    return sortMap(map, new ReverseComparator<Map.Entry<K, V>>(new PropertyComparator<Map.Entry<K, V>>("key")));
}
 
Example #4
Source File: SortUtil.java    From feilong-core with Apache License 2.0 3 votes vote down vote up
/**
 * 根据value 来倒序排序(desc).
 * 
 * <h3>注意:</h3>
 * <blockquote>
 * <ol>
 * <li><span style="color:red">原 <code>map</code> 的顺序不变</span></li>
 * </ol>
 * </blockquote>
 * 
 * <h3>示例:</h3>
 * <blockquote>
 * 
 * <pre class="code">
 * Map{@code <String, Comparable>} map = new LinkedHashMap{@code <>}();
 * 
 * map.put("a", 123);
 * map.put("c", 345);
 * map.put("b", 8);
 * 
 * LOGGER.debug(JsonUtil.format(SortUtil.sortMapByValueDesc(map)));
 * </pre>
 * 
 * <b>返回:</b>
 * 
 * <pre class="code">
 * {
 * "c": 345,
 * "a": 123,
 * "b": 8
 * }
 * </pre>
 * 
 * </blockquote>
 * 
 * @param <K>
 *            the key type
 * @param <V>
 *            the value type
 * @param map
 *            the map
 * @return 如果 <code>map</code> 是null,返回 {@link Collections#emptyMap()}<br>
 * @see #sortMap(Map, Comparator)
 * @since 1.8.0 move from MapUtil
 * @since 1.8.7 change method name from sortByValueDesc
 */
public static <K, V extends Comparable<V>> Map<K, V> sortMapByValueDesc(Map<K, V> map){
    if (null == map){
        return emptyMap();
    }
    return sortMap(map, new ReverseComparator<Map.Entry<K, V>>(new PropertyComparator<Map.Entry<K, V>>("value")));
}