About 8,360,000 results
Open links in new tab
  1. How to use Comparator in Java to sort - Stack Overflow

    Again, to use Collections.sort, you need one of these to be true: The type must be Comparable (use the 1-arg sort) A Comparator for the type must be provided (use the 2-args sort) When to use …

  2. java - How to use the Comparator interface - Stack Overflow

    Apr 21, 2013 · To use the Comparator interface you have to implement it and pass it as an anonymous class to Collections.sort (List list, Comparator c) as the second parameter.

  3. What is the use of Comparator.comparing in respect to Comparator

    Mar 20, 2022 · Comparator.comparing(Function<? super T, ? extends U> key) returns a Comparator<T> that compares by that sort key. The main difference is that compare method provides a single point …

  4. How do I use Comparator to define a custom sort order?

    Mar 9, 2011 · Here it is not sort by alphabetic order. I want to use my custom sorting order like Red car come first, then Blue, etc. For that I try to use Java Comparator and Comparable but it allows to sort …

  5. When to use Comparable and Comparator - Stack Overflow

    Feb 15, 2010 · Use Comparable if you want to define a default (natural) ordering behaviour of the object in question, a common practice is to use a technical or natural (database?) identifier of the object for …

  6. java - How do I use a PriorityQueue? - Stack Overflow

    Mar 25, 2009 · Use the constructor overload which takes a Comparator<? super E> comparator and pass in a comparator which compares in the appropriate way for your sort order. If you give an …

  7. How to use a Java 8 lambda to sort a Stream in reverse order?

    This can easily be done using Java 8 and the use of a reversed Comparator. I have created a list of files from a directory, which I display unsorted, sorted and reverse sorted using a simple Comparator for …

  8. Java : Comparable vs Comparator - Stack Overflow

    Comparator provides a way for you to provide custom comparison logic for types that you have no control over. Comparable allows you to specify how objects that you are implementing get compared. …

  9. sorting - Reverse a comparator in Java 8 - Stack Overflow

    Oct 7, 2015 · I use for it java.util.stream.Stream.sorted(Comparator) method. Here is a description according Java API: Returns a stream consisting of the elements of this stream, sorted according to …

  10. java comparator, how to sort by integer? - Stack Overflow

    For example, a Comparator<Dog> to sort Dog instances descending by age could be created with the following: Comparable.comparingToInt(Dog::getDogAge).reversed(); The function take a lambda …