本文整理汇总了Java中org.apache.commons.collections.ComparatorUtils.naturalComparator方法的典型用法代码示例。如果您正苦于以下问题:Java ComparatorUtils.naturalComparator方法的具体用法?Java ComparatorUtils.naturalComparator怎么用?Java ComparatorUtils.naturalComparator使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.collections.ComparatorUtils
的用法示例。
在下文中一共展示了ComparatorUtils.naturalComparator方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: partialSort
import org.apache.commons.collections.ComparatorUtils; //导入方法依赖的package包/类
/**
* Partial Sort: sorts in place an array of Objects using a given Comparator,
* but only enough so that the N biggest (or smallest) items are at the start
* of the array. Not a stable sort, unless the Comparator is so contrived.
*
* @param items will be partially-sorted in place
* @param comp a Comparator; null means use natural comparison
*/
static <T> void partialSort(T[] items, Comparator<T> comp, int limit)
{
if (comp == null) {
//noinspection unchecked
comp = (Comparator<T>) ComparatorUtils.naturalComparator();
}
new Quicksorter<T>(items, comp).partialSort(limit);
}
示例2: doPartialSort
import org.apache.commons.collections.ComparatorUtils; //导入方法依赖的package包/类
@SuppressWarnings({"unchecked"})
private void doPartialSort(Object[] items, boolean descending, int limit)
{
Comparator<Object> comp = ComparatorUtils.naturalComparator();
if (descending) {
comp = ComparatorUtils.reversedComparator(comp);
}
FunUtil.partialSort(items, comp, limit);
}
示例3: doPartialSort
import org.apache.commons.collections.ComparatorUtils; //导入方法依赖的package包/类
private void doPartialSort(Object[] items, boolean descending, int limit)
{
Comparator comp = ComparatorUtils.naturalComparator();
if (descending) {
comp = ComparatorUtils.reversedComparator(comp);
}
FunUtil.partialSort(items, comp, limit);
}
示例4: speedTest
import org.apache.commons.collections.ComparatorUtils; //导入方法依赖的package包/类
private void speedTest(int length, int limit) {
System.out.println(
"sorting the max " + limit + " of " + length + " random Integers");
// random input, 3 copies
// repeated keys
Integer[] vec1 = newRandomIntegers(length, 0, length / 5);
Integer[] vec2 = new Integer[length];
Integer[] vec3 = new Integer[length];
System.arraycopy(vec1, 0, vec2, 0, length);
System.arraycopy(vec1, 0, vec3, 0, length);
// full sort vec1
long now = System.currentTimeMillis();
Arrays.sort(vec1);
long dt = System.currentTimeMillis() - now;
System.out.println(" full mergesort took " + dt + " msecs");
// partial sort vec2
now = System.currentTimeMillis();
doPartialSort(vec2, true, limit);
dt = System.currentTimeMillis() - now;
System.out.println(" partial quicksort took " + dt + " msecs");
// stable partial sort vec3
Comparator comp =
new ReverseComparator(ComparatorUtils.naturalComparator());
List<Integer> vec3List = Arrays.asList(vec3);
now = System.currentTimeMillis();
FunUtil.stablePartialSort(vec3List, comp, limit);
dt = System.currentTimeMillis() - now;
System.out.println(" stable partial quicksort took " + dt + " msecs");
}
示例5: speedTest
import org.apache.commons.collections.ComparatorUtils; //导入方法依赖的package包/类
private void speedTest(Logger logger, int length, int limit) {
logger.debug(
"sorting the max " + limit + " of " + length + " random Integers");
// random input, 3 copies
// repeated keys
Integer[] vec1 = newRandomIntegers(length, 0, length / 5);
Integer[] vec2 = vec1.clone();
Integer[] vec3 = vec1.clone();
Integer[] vec4 = vec1.clone();
// full sort vec1
long now = System.currentTimeMillis();
Arrays.sort(vec1);
long dt = System.currentTimeMillis() - now;
logger.debug(" full mergesort took " + dt + " msecs");
// partial sort vec2
now = System.currentTimeMillis();
doPartialSort(vec2, true, limit);
dt = System.currentTimeMillis() - now;
logger.debug(" partial quicksort took " + dt + " msecs");
// marc's stable partial quicksort vec3
@SuppressWarnings({"unchecked"})
Comparator<Integer> comp =
new ReverseComparator(ComparatorUtils.naturalComparator());
List<Integer> vec3List = Arrays.asList(vec3);
now = System.currentTimeMillis();
FunUtil.stablePartialSort(vec3List, comp, limit, 2);
dt = System.currentTimeMillis() - now;
logger.debug(" marc's stable partial quicksort took " + dt + " msecs");
// julian's algorithm stable partial sort vec4
@SuppressWarnings({"unchecked"})
List<Integer> vec4List = Arrays.asList(vec4);
now = System.currentTimeMillis();
FunUtil.stablePartialSort(vec4List, comp, limit, 4);
dt = System.currentTimeMillis() - now;
logger.debug(" julian's stable partial sort took " + dt + " msecs");
}
示例6: partialSort
import org.apache.commons.collections.ComparatorUtils; //导入方法依赖的package包/类
/**
* Partial Sort: sorts in place an array of Objects using a given Comparator,
* but only enough so that the N biggest (or smallest) items are at the start
* of the array. Not a stable sort, unless the Comparator is so contrived.
*
* @param items will be partially-sorted in place
* @param comp a Comparator; null means use natural comparison
* @param limit
*/
static void partialSort(Object[] items, Comparator comp, int limit)
{
if (comp == null) {
comp = ComparatorUtils.naturalComparator();
}
new Quicksorter(items, comp).partialSort(limit);
}