Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since <a href="//docs.oracle.com/javase/8/docs/api/java/util/Date.html" rel="noreferrer"><code>Date</code></a> implements <a href="//docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html" rel="noreferrer"><code>Comparable</code></a>, it has a <code>compareTo</code> method just like <code>String</code> does.</p> <p>So your custom <a href="//docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" rel="noreferrer"><code>Comparator</code></a> could look like this:</p> <pre><code>public class CustomComparator implements Comparator&lt;MyObject&gt; { @Override public int compare(MyObject o1, MyObject o2) { return o1.getStartDate().compareTo(o2.getStartDate()); } } </code></pre> <p>The <code>compare()</code> method must return an <code>int</code>, so you couldn't directly return a <code>boolean</code> like you were planning to anyway.</p> <p>Your sorting code would be just about like you wrote:</p> <pre><code>Collections.sort(Database.arrayList, new CustomComparator()); </code></pre> <p>A slightly shorter way to write all this, if you don't need to reuse your comparator, is to write it as an inline anonymous class:</p> <pre><code>Collections.sort(Database.arrayList, new Comparator&lt;MyObject&gt;() { @Override public int compare(MyObject o1, MyObject o2) { return o1.getStartDate().compareTo(o2.getStartDate()); } }); </code></pre> <hr> <h1>Since <a href="/questions/tagged/java-8" class="post-tag" title="show questions tagged &#39;java-8&#39;" rel="tag">java-8</a></h1> <p>You can now write the last example in a shorter form by using a <a href="//docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html" rel="noreferrer">lambda expression</a> for the <code>Comparator</code>:</p> <pre><code>Collections.sort(Database.arrayList, (o1, o2) -&gt; o1.getStartDate().compareTo(o2.getStartDate())); </code></pre> <p>And <code>List</code> has a <a href="//docs.oracle.com/javase/8/docs/api/java/util/List.html#sort-java.util.Comparator-" rel="noreferrer"><code>sort(Comparator)</code></a> method, so you can shorten this even further:</p> <pre><code>Database.arrayList.sort((o1, o2) -&gt; o1.getStartDate().compareTo(o2.getStartDate())); </code></pre> <p>This is such a common idiom that there's <a href="//docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#comparing-java.util.function.Function-" rel="noreferrer">a built-in method</a> to generate a <code>Comparator</code> for a class with a <code>Comparable</code> key:</p> <pre><code>Database.arrayList.sort(Comparator.comparing(MyObject::getStartDate)); </code></pre> <p>All of these are equivalent forms.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload