当前位置: 首页>>代码示例>>Java>>正文


Java Comparator.naturalOrder方法代码示例

本文整理汇总了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);
}
 
开发者ID:Juraji,项目名称:Biliomi,代码行数:34,代码来源:XmlElementPathBeanComparator.java

示例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;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:14,代码来源:SortedOps.java

示例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);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:13,代码来源:BasicTest.java

示例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());
    }
}
 
开发者ID:kostovhg,项目名称:SoftUni,代码行数:10,代码来源:DisplayCommand.java

示例5: create

import java.util.Comparator; //导入方法依赖的package包/类
public static <T extends Comparable<T>> RelativeComparator<T> create() {
  return new RelativeComparator<T>(Comparator.naturalOrder());
}
 
开发者ID:XDean,项目名称:Java-EX,代码行数:4,代码来源:RelativeComparator.java

示例6: testNaturalOrderComparator

import java.util.Comparator; //导入方法依赖的package包/类
public void testNaturalOrderComparator() {
    Comparator<String> comp = Comparator.naturalOrder();

    assertComparisons(stringValues, comp, comparisons);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:6,代码来源:BasicTest.java

示例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)
    );
}
 
开发者ID:yegor256,项目名称:cactoos,代码行数:17,代码来源:Sorted.java

示例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));
}
 
开发者ID:yegor256,项目名称:cactoos,代码行数:14,代码来源:Sorted.java

示例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);
}
 
开发者ID:yegor256,项目名称:cactoos,代码行数:14,代码来源:Sorted.java

示例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);
}
 
开发者ID:yegor256,项目名称:cactoos,代码行数:14,代码来源:Sorted.java

示例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);
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:12,代码来源:CollectionUtils.java


注:本文中的java.util.Comparator.naturalOrder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。