Java Code Examples for java.util.Comparator.naturalOrder()

The following are Jave code examples for showing how to use naturalOrder() of the java.util.Comparator class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Biliomi   File: XmlElementPathBeanComparator.java   View Source Code Vote up 6 votes
@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);
}
 
Example 2
Project: OpenJSharp   File: SortedOps.java   View Source Code Vote up 5 votes
/**
 * 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;
}
 
Example 3
Project: jdk8u-jdk   File: SortedOps.java   View Source Code Vote up 5 votes
/**
 * 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;
}
 
Example 4
Project: jdk8u-jdk   File: BasicTest.java   View Source Code Vote up 5 votes
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);
}
 
Example 5
Project: SoftUni   File: DisplayCommand.java   View Source Code Vote up 5 votes
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());
    }
}
 
Example 6
Project: openjdk-jdk10   File: SortedOps.java   View Source Code Vote up 5 votes
/**
 * 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;
}
 
Example 7
Project: openjdk-jdk10   File: BasicTest.java   View Source Code Vote up 5 votes
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);
}
 
Example 8
Project: Java-EX   File: RelativeComparator.java   View Source Code Vote up 4 votes
public static <T extends Comparable<T>> RelativeComparator<T> create() {
  return new RelativeComparator<T>(Comparator.naturalOrder());
}
 
Example 9
Project: jdk8u-jdk   File: BasicTest.java   View Source Code Vote up 4 votes
public void testNaturalOrderComparator() {
    Comparator<String> comp = Comparator.naturalOrder();

    assertComparisons(stringValues, comp, comparisons);
}
 
Example 10
Project: openjdk-jdk10   File: BasicTest.java   View Source Code Vote up 4 votes
public void testNaturalOrderComparator() {
    Comparator<String> comp = Comparator.naturalOrder();

    assertComparisons(stringValues, comp, comparisons);
}
 
Example 11
Project: cactoos   File: Sorted.java   View Source Code Vote up 3 votes
/**
 * 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)
    );
}
 
Example 12
Project: cactoos   File: Sorted.java   View Source Code Vote up 2 votes
/**
 * 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));
}
 
Example 13
Project: cactoos   File: Sorted.java   View Source Code Vote up 2 votes
/**
 * 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);
}
 
Example 14
Project: cactoos   File: Sorted.java   View Source Code Vote up 2 votes
/**
 * 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);
}
 
Example 15
Project: freecol   File: CollectionUtils.java   View Source Code Vote up 2 votes
/**
 * 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);
}
 
Example 16
Project: freecol   File: CollectionUtils.java   View Source Code Vote up 2 votes
/**
 * Convenience function to convert a collection to a sorted list.
 *
 * @param <T> The collection member type.
 * @param c The {@code Collection} to convert.
 * @return A list of the stream contents.
 */
public static <T extends Comparable<? super T>> List<T> sort(Collection<T> c) {
    final Comparator<T> comparator = Comparator.naturalOrder();
    return sort_internal(c.stream(), comparator);
}
 
Example 17
Project: freecol   File: CollectionUtils.java   View Source Code Vote up 2 votes
/**
 * Convenience function to collect a stream to a list.
 *
 * @param <T> The stream member type.
 * @param stream The {@code Stream} to collect.
 * @return A list of the stream contents.
 */
public static <T extends Comparable<? super T>> List<T> sort(Stream<T> stream) {
    final Comparator<T> comparator = Comparator.naturalOrder();
    return (stream == null) ? Collections.<T>emptyList()
        : sort_internal(stream, comparator);
}
 
Example 18
Project: FreeCol   File: CollectionUtils.java   View Source Code Vote up 2 votes
/**
 * 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);
}
 
Example 19
Project: FreeCol   File: CollectionUtils.java   View Source Code Vote up 2 votes
/**
 * Convenience function to convert a collection to a sorted list.
 *
 * @param <T> The collection member type.
 * @param c The {@code Collection} to convert.
 * @return A list of the stream contents.
 */
public static <T extends Comparable<? super T>> List<T> sort(Collection<T> c) {
    final Comparator<T> comparator = Comparator.naturalOrder();
    return sort_internal(c.stream(), comparator);
}
 
Example 20
Project: FreeCol   File: CollectionUtils.java   View Source Code Vote up 2 votes
/**
 * Convenience function to collect a stream to a list.
 *
 * @param <T> The stream member type.
 * @param stream The {@code Stream} to collect.
 * @return A list of the stream contents.
 */
public static <T extends Comparable<? super T>> List<T> sort(Stream<T> stream) {
    final Comparator<T> comparator = Comparator.naturalOrder();
    return (stream == null) ? Collections.<T>emptyList()
        : sort_internal(stream, comparator);
}