本文整理汇总了Java中java.util.Comparator.naturalOrder方法的典型用法代码示例。如果您正苦于以下问题:Java Comparator.naturalOrder方法的具体用法?Java Comparator.naturalOrder怎么用?Java Comparator.naturalOrder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Comparator
的用法示例。
在下文中一共展示了Comparator.naturalOrder方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compare
import java.util.Comparator; //导入方法依赖的package包/类
@Override
public int compare(T o1, T o2) {
Comparable obj1 = null;
Comparable obj2 = null;
try {
obj1 = (Comparable) PropertyUtils.getProperty(o1, this.propertyPath);
obj2 = (Comparable) PropertyUtils.getProperty(o2, this.propertyPath);
} catch (NestedNullException ignored) {
// Ignored, als het property NULL is, dan vergelijken met NULL
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IllegalArgumentException("Could not retrieve property " + this.propertyPath, e);
}
Comparator<Comparable<Object>> objectComparator = null;
if ((obj1 != null && obj2 != null) && obj1 instanceof String) {
obj1 = ((String) obj1).toLowerCase().trim();
obj2 = ((String) obj2).toLowerCase().trim();
if (!StringUtils.isEmpty((String) obj1) && !StringUtils.isEmpty((String) obj2)) {
if (StringUtils.isNumeric((String) obj1) && StringUtils.isNumeric((String) obj2)) {
objectComparator = Comparator.comparingDouble(o -> new Double(String.valueOf(o)));
}
}
}
if (objectComparator == null) {
objectComparator = Comparator.naturalOrder();
}
//noinspection unchecked
return Comparator.nullsLast(objectComparator).compare(obj1, obj2);
}
示例2: OfRef
import java.util.Comparator; //导入方法依赖的package包/类
/**
* Sort using natural order of {@literal <T>} which must be
* {@code Comparable}.
*/
OfRef(AbstractPipeline<?, T, ?> upstream) {
super(upstream, StreamShape.REFERENCE,
StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
this.isNaturalSort = true;
// Will throw CCE when we try to sort if T is not Comparable
@SuppressWarnings("unchecked")
Comparator<? super T> comp = (Comparator<? super T>) Comparator.naturalOrder();
this.comparator = comp;
}
示例3: testComposeComparator
import java.util.Comparator; //导入方法依赖的package包/类
public void testComposeComparator() {
// Longer string in front
Comparator<String> first = (s1, s2) -> s2.length() - s1.length();
Comparator<String> second = Comparator.naturalOrder();
Comparator<String> composed = first.thenComparing(second);
assertTrue(composed.compare("abcdefg", "abcdef") < 0);
assertTrue(composed.compare("abcdef", "abcdefg") > 0);
assertTrue(composed.compare("abcdef", "abcdef") == 0);
assertTrue(composed.compare("abcdef", "ghijkl") < 0);
assertTrue(composed.compare("ghijkl", "abcdefg") > 0);
}
示例4: createCourseComparator
import java.util.Comparator; //导入方法依赖的package包/类
private Comparator<Course> createCourseComparator (String sortType) {
if(sortType.equalsIgnoreCase("descending")) {
return Comparator.reverseOrder();
} else if (sortType.equalsIgnoreCase("ascending")) {
return Comparator.naturalOrder();
} else {
throw new InvalidInputException(this.getInput());
}
}
示例5: create
import java.util.Comparator; //导入方法依赖的package包/类
public static <T extends Comparable<T>> RelativeComparator<T> create() {
return new RelativeComparator<T>(Comparator.naturalOrder());
}
示例6: testNaturalOrderComparator
import java.util.Comparator; //导入方法依赖的package包/类
public void testNaturalOrderComparator() {
Comparator<String> comp = Comparator.naturalOrder();
assertComparisons(stringValues, comp, comparisons);
}
示例7: Sorted
import java.util.Comparator; //导入方法依赖的package包/类
/**
* Ctor.
*
* <p>If you're using this ctor you must be sure that type {@code T}
* implements {@link Comparable} interface. Otherwise, there will be
* a type casting exception in runtime.</p>
*
* @param src The underlying collection
*/
@SuppressWarnings("unchecked")
public Sorted(final Iterable<T> src) {
this(
(Comparator<T>) Comparator.naturalOrder(),
new CollectionOf<>(src)
);
}
示例8: Sorted
import java.util.Comparator; //导入方法依赖的package包/类
/**
* Ctor.
*
* <p>If you're using this ctor you must be sure that type {@code T}
* implements {@link Comparable} interface. Otherwise, there will be
* a type casting exception in runtime.</p>
*
* @param src The underlying collection
*/
@SuppressWarnings("unchecked")
public Sorted(final Iterable<T> src) {
this((Comparator<T>) Comparator.naturalOrder(), new ListOf<>(src));
}
示例9: Sorted
import java.util.Comparator; //导入方法依赖的package包/类
/**
* Ctor.
*
* <p>If you're using this ctor you must be sure that type {@code T}
* implements {@link Comparable} interface. Otherwise, there will be
* a type casting exception in runtime.</p>
*
* @param src The underlying iterable
*/
@SuppressWarnings("unchecked")
public Sorted(final Iterable<T> src) {
this((Comparator<T>) Comparator.naturalOrder(), src);
}
示例10: Sorted
import java.util.Comparator; //导入方法依赖的package包/类
/**
* Ctor.
*
* <p>If you're using this ctor you must be sure that type {@code T}
* implements {@link Comparable} interface. Otherwise, there will be
* a type casting exception in runtime.</p>
*
* @param items The underlying iterator
*/
@SuppressWarnings("unchecked")
public Sorted(final Iterator<T> items) {
this((Comparator<T>) Comparator.naturalOrder(), items);
}
示例11: sort
import java.util.Comparator; //导入方法依赖的package包/类
/**
* Convenience function to convert an array to a sorted list.
*
* @param <T> The array member type.
* @param array The array to convert.
* @return A list of the stream contents.
*/
public static <T extends Comparable<? super T>> List<T> sort(T[] array) {
final Comparator<T> comparator = Comparator.naturalOrder();
return sort_internal(Arrays.stream(array), comparator);
}