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

The following are Jave code examples for showing how to use reversed() 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: jdk8u-jdk   File: BasicTest.java   View Source Code Vote up 9 votes
public void testNullsFirst() {
    Comparator<String> strcmp = Comparator.nullsFirst(Comparator.naturalOrder());
    Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
                                       .thenComparing(People::getFirstName, strcmp);
    // Mary.null vs Mary.Cook - solve by last name
    assertComparison(cmp, people[6], people[5]);
    // John.null vs Mary.null - solve by first name
    assertComparison(cmp, people[7], people[6]);

    // More than one thenComparing
    strcmp = Comparator.nullsFirst(Comparator.comparingInt(String::length)
                                             .thenComparing(String.CASE_INSENSITIVE_ORDER));
    assertComparison(strcmp, null, "abc");
    assertComparison(strcmp, "ab", "abc");
    assertComparison(strcmp, "abc", "def");
    assertEquals(0, strcmp.compare("abc", "ABC"));

    // Ensure reverse still handle null properly
    Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp2, "abc", null);
    assertComparison(strcmp2, "abc", "ab");
    assertComparison(strcmp2, "def", "abc");
    assertComparison(strcmp2, "ABC", "abc");

    // Considering non-null values to be equal
    Comparator<String> blind = Comparator.nullsFirst(null);
    assertComparison(blind, null, "abc");
    assertEquals(0, blind.compare("abc", "def"));
    // reverse still consider non-null values to be equal
    strcmp = blind.reversed();
    assertComparison(strcmp, "abc", null);
    assertEquals(0, strcmp.compare("abc", "def"));
    // chain with another comparator to compare non-nulls
    strcmp = blind.thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp, null, "abc");
    assertComparison(strcmp, "abc", "def");
}
 
Example 2
Project: ARCLib   File: HistoricStore.java   View Source Code Vote up 6 votes
public List<Revision> getRevisions(String id, Params params) {

        AuditReader reader = getAuditReader();

        List<Number> revisionNumbers = reader.getRevisions(type, id);
        Map<Number, Revision> revisionMap = reader.findRevisions(Revision.class, asSet(revisionNumbers));
        Collection<Revision> revisions = revisionMap.values();

        Comparator<Revision> timestampComparator = Comparator.comparingLong(Revision::getTimestamp);
        Comparator<Revision> authorComparator = Comparator.comparing(Revision::getAuthor);

        Comparator<Revision> comparator;
        if (AUTHOR.equals(params.getSort())) {
            comparator = authorComparator;
        } else {
            comparator = timestampComparator;
        }

        if (params.getOrder() == Order.DESC) {
            comparator = comparator.reversed();
        }

        return Ordering.from(comparator)
                       .sortedCopy(revisions);
    }
 
Example 3
Project: jpa2ddl   File: FileResolver.java   View Source Code Vote up 6 votes
static List<Path> resolveExistingMigrations(File migrationsDir, boolean reversed, boolean onlySchemaMigrations) {
	if (!migrationsDir.exists()) {
		migrationsDir.mkdirs();
	}

	File[] files = migrationsDir.listFiles();

	if (files == null) {
		return Collections.emptyList();
	}

	Comparator<Path> pathComparator = Comparator.comparingInt(FileResolver::compareVersionedMigrations);
	if (reversed) {
		pathComparator = pathComparator.reversed();
	}
	return Arrays.stream(files)
			.map(File::toPath)
			.filter(path -> !onlySchemaMigrations || SCHEMA_FILENAME_PATTERN.matcher(path.getFileName().toString()).matches())
			.sorted(pathComparator)
			.collect(Collectors.toList());
}
 
Example 4
Project: gauravbytes   File: MethodReferenceExample.java   View Source Code Vote up 5 votes
private static void instanceMethodReferenceOfArbitraryObject() {
	System.err.println("Instance method reference of ArbitraryObject");
	Comparator<String> stringIgnoreCase = String::compareToIgnoreCase;
	// this is equivalent to below
	Comparator<String> stringIgnoreCase2 = (first, second) -> first
	    .compareToIgnoreCase(second);

	Comparator<String> reverseOrder = stringIgnoreCase.reversed();

	List<String> values = Arrays.asList("Gaurav", "Sunil", "Anil", "Rajesh", "sajjad");
	System.out.println(values);

	Collections.sort(values, stringIgnoreCase);
	System.out.println(values);
}
 
Example 5
Project: jdk8u-jdk   File: BasicTest.java   View Source Code Vote up 5 votes
public void testReverseComparator() {
    Comparator<String> cmpr = Comparator.reverseOrder();
    Comparator<String> cmp = cmpr.reversed();

    assertEquals(cmp.reversed(), cmpr);
    assertEquals(0, cmp.compare("a", "a"));
    assertEquals(0, cmpr.compare("a", "a"));
    assertTrue(cmp.compare("a", "b") < 0);
    assertTrue(cmpr.compare("a", "b") > 0);
    assertTrue(cmp.compare("b", "a") > 0);
    assertTrue(cmpr.compare("b", "a") < 0);
}
 
Example 6
Project: jdk8u-jdk   File: BasicTest.java   View Source Code Vote up 5 votes
public void testReverseComparator2() {
    Comparator<String> cmp = (s1, s2) -> s1.length() - s2.length();
    Comparator<String> cmpr = cmp.reversed();

    assertEquals(cmpr.reversed(), cmp);
    assertEquals(0, cmp.compare("abc", "def"));
    assertEquals(0, cmpr.compare("abc", "def"));
    assertTrue(cmp.compare("abcd", "def") > 0);
    assertTrue(cmpr.compare("abcd", "def") < 0);
    assertTrue(cmp.compare("abc", "defg") < 0);
    assertTrue(cmpr.compare("abc", "defg") > 0);
}
 
Example 7
Project: jdk8u-jdk   File: BasicTest.java   View Source Code Vote up 5 votes
public void testNullsLast() {
    Comparator<String> strcmp = Comparator.nullsLast(Comparator.naturalOrder());
    Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
                                       .thenComparing(People::getFirstName, strcmp);
    // Mary.null vs Mary.Cook - solve by last name
    assertComparison(cmp, people[5], people[6]);
    // John.null vs Mary.null - solve by first name
    assertComparison(cmp, people[7], people[6]);

    // More than one thenComparing
    strcmp = Comparator.nullsLast(Comparator.comparingInt(String::length)
                                            .thenComparing(String.CASE_INSENSITIVE_ORDER));
    assertComparison(strcmp, "abc", null);
    assertComparison(strcmp, "ab", "abc");
    assertComparison(strcmp, "abc", "def");

    // Ensure reverse still handle null properly
    Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp2, null, "abc");
    assertComparison(strcmp2, "abc", "ab");
    assertComparison(strcmp2, "def", "abc");
    assertComparison(strcmp2, "ABC", "abc");

    // Considering non-null values to be equal
    Comparator<String> blind = Comparator.nullsLast(null);
    assertComparison(blind, "abc", null);
    assertEquals(0, blind.compare("abc", "def"));
    // reverse still consider non-null values to be equal
    strcmp = blind.reversed();
    assertComparison(strcmp, null, "abc");
    assertEquals(0, strcmp.compare("abc", "def"));
    // chain with another comparator to compare non-nulls
    strcmp = blind.thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp, "abc", null);
    assertComparison(strcmp, "abc", "def");
}
 
Example 8
Project: openjdk-jdk10   File: BasicTest.java   View Source Code Vote up 5 votes
public void testReverseComparator() {
    Comparator<String> cmpr = Comparator.reverseOrder();
    Comparator<String> cmp = cmpr.reversed();

    assertEquals(cmp.reversed(), cmpr);
    assertEquals(0, cmp.compare("a", "a"));
    assertEquals(0, cmpr.compare("a", "a"));
    assertTrue(cmp.compare("a", "b") < 0);
    assertTrue(cmpr.compare("a", "b") > 0);
    assertTrue(cmp.compare("b", "a") > 0);
    assertTrue(cmpr.compare("b", "a") < 0);
}
 
Example 9
Project: openjdk-jdk10   File: BasicTest.java   View Source Code Vote up 5 votes
public void testReverseComparator2() {
    Comparator<String> cmp = (s1, s2) -> s1.length() - s2.length();
    Comparator<String> cmpr = cmp.reversed();

    assertEquals(cmpr.reversed(), cmp);
    assertEquals(0, cmp.compare("abc", "def"));
    assertEquals(0, cmpr.compare("abc", "def"));
    assertTrue(cmp.compare("abcd", "def") > 0);
    assertTrue(cmpr.compare("abcd", "def") < 0);
    assertTrue(cmp.compare("abc", "defg") < 0);
    assertTrue(cmpr.compare("abc", "defg") > 0);
}
 
Example 10
Project: openjdk-jdk10   File: BasicTest.java   View Source Code Vote up 5 votes
public void testNullsFirst() {
    Comparator<String> strcmp = Comparator.nullsFirst(Comparator.naturalOrder());
    Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
                                       .thenComparing(People::getFirstName, strcmp);
    // Mary.null vs Mary.Cook - solve by last name
    assertComparison(cmp, people[6], people[5]);
    // John.null vs Mary.null - solve by first name
    assertComparison(cmp, people[7], people[6]);

    // More than one thenComparing
    strcmp = Comparator.nullsFirst(Comparator.comparingInt(String::length)
                                             .thenComparing(String.CASE_INSENSITIVE_ORDER));
    assertComparison(strcmp, null, "abc");
    assertComparison(strcmp, "ab", "abc");
    assertComparison(strcmp, "abc", "def");
    assertEquals(0, strcmp.compare("abc", "ABC"));

    // Ensure reverse still handle null properly
    Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp2, "abc", null);
    assertComparison(strcmp2, "abc", "ab");
    assertComparison(strcmp2, "def", "abc");
    assertComparison(strcmp2, "ABC", "abc");

    // Considering non-null values to be equal
    Comparator<String> blind = Comparator.nullsFirst(null);
    assertComparison(blind, null, "abc");
    assertEquals(0, blind.compare("abc", "def"));
    // reverse still consider non-null values to be equal
    strcmp = blind.reversed();
    assertComparison(strcmp, "abc", null);
    assertEquals(0, strcmp.compare("abc", "def"));
    // chain with another comparator to compare non-nulls
    strcmp = blind.thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp, null, "abc");
    assertComparison(strcmp, "abc", "def");
}
 
Example 11
Project: openjdk-jdk10   File: BasicTest.java   View Source Code Vote up 5 votes
public void testNullsLast() {
    Comparator<String> strcmp = Comparator.nullsLast(Comparator.naturalOrder());
    Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
                                       .thenComparing(People::getFirstName, strcmp);
    // Mary.null vs Mary.Cook - solve by last name
    assertComparison(cmp, people[5], people[6]);
    // John.null vs Mary.null - solve by first name
    assertComparison(cmp, people[7], people[6]);

    // More than one thenComparing
    strcmp = Comparator.nullsLast(Comparator.comparingInt(String::length)
                                            .thenComparing(String.CASE_INSENSITIVE_ORDER));
    assertComparison(strcmp, "abc", null);
    assertComparison(strcmp, "ab", "abc");
    assertComparison(strcmp, "abc", "def");

    // Ensure reverse still handle null properly
    Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp2, null, "abc");
    assertComparison(strcmp2, "abc", "ab");
    assertComparison(strcmp2, "def", "abc");
    assertComparison(strcmp2, "ABC", "abc");

    // Considering non-null values to be equal
    Comparator<String> blind = Comparator.nullsLast(null);
    assertComparison(blind, "abc", null);
    assertEquals(0, blind.compare("abc", "def"));
    // reverse still consider non-null values to be equal
    strcmp = blind.reversed();
    assertComparison(strcmp, null, "abc");
    assertEquals(0, strcmp.compare("abc", "def"));
    // chain with another comparator to compare non-nulls
    strcmp = blind.thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp, "abc", null);
    assertComparison(strcmp, "abc", "def");
}
 
Example 12
Project: oryx2   File: Pairs.java   View Source Code Vote up 5 votes
/**
 * @param order whether to sort ascending or descending; {@code null} comes last
 * @param <C> type of first element in {@link Pair}s to be compared
 * @param <D> type of second element in {@link Pair}s to be compared
 * @return an ordering on {@link Pair}s by first element as a {@link Comparator}
 */
public static <C extends Comparable<C>,D> Comparator<Pair<C,D>> orderByFirst(SortOrder order) {
  Comparator<Pair<C,D>> ordering = Comparator.comparing(Pair::getFirst);
  if (order == SortOrder.DESCENDING) {
    ordering = ordering.reversed();
  }
  return Comparator.nullsLast(ordering);
}
 
Example 13
Project: oryx2   File: Pairs.java   View Source Code Vote up 5 votes
/**
 * @param order whether to sort ascending or descending; {@code null} comes last
 * @param <C> type of first element in {@link Pair}s to be compared
 * @param <D> type of second element in {@link Pair}s to be compared
 * @return an ordering on {@link Pair}s by second element as a {@link Comparator}
 */
public static <C,D extends Comparable<D>> Comparator<Pair<C,D>> orderBySecond(SortOrder order) {
  Comparator<Pair<C,D>> ordering = Comparator.comparing(Pair::getSecond);
  if (order == SortOrder.DESCENDING) {
    ordering = ordering.reversed();
  }
  return Comparator.nullsLast(ordering);
}
 
Example 14
Project: Biliomi   File: XmlElementPathBeanComparator.java   View Source Code Vote up 4 votes
public static <T> Comparator<T> forSortDirective(RestSortDirective sortDirective, Class<T> rootClass) {
  Comparator<T> comparator = new XmlElementPathBeanComparator<>(sortDirective.getProperty(), rootClass);
  return (sortDirective.isDescending() ? comparator.reversed() : comparator);
}
 
Example 15
Project: spring-boot-readiness   File: OrderCompare.java   View Source Code Vote up 4 votes
@Override
public Comparator<HealthResponse> apply(Order order) {
    Comparator<HealthResponse> comparator = comparators.getOrDefault(order.getProperty(), SORT_BY_SERVICE);
    return order.getDirection().isDescending() ? comparator.reversed() : comparator;
}