當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。