Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <code>People implements Comparable&lt;People&gt;</code> instead; this defines the natural ordering for <code>People</code>.</p> <p>A <code>Comparator&lt;People&gt;</code> can also be defined in addition, but <code>People implements Comparator&lt;People&gt;</code> is not the right way of doing things.</p> <p>The two overloads for <code>Collections.sort</code> are different:</p> <ul> <li><code>&lt;T extends Comparable&lt;? super T&gt;&gt; void sort(List&lt;T&gt; list)</code> <ul> <li>Sorts <code>Comparable</code> objects using their natural ordering</li> </ul></li> <li><code>&lt;T&gt; void sort(List&lt;T&gt; list, Comparator&lt;? super T&gt; c)</code> <ul> <li>Sorts whatever using a compatible <code>Comparator</code></li> </ul></li> </ul> <p>You're confusing the two by trying to sort a <code>Comparator</code> (which is again why it doesn't make sense that <code>Person implements Comparator&lt;Person&gt;</code>). Again, to use <code>Collections.sort</code>, you need one of these to be true:</p> <ul> <li>The type must be <code>Comparable</code> (use the 1-arg <code>sort</code>)</li> <li>A <code>Comparator</code> for the type must be provided (use the 2-args <code>sort</code>)</li> </ul> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/2266827/when-to-use-comparable-vs-comparator">When to use Comparable vs Comparator</a></li> <li><a href="https://stackoverflow.com/questions/1814095/sorting-an-arraylist-of-contacts">Sorting an ArrayList of Contacts</a></li> </ul> <hr> <p>Also, <strong>do not use raw types in new code</strong>. Raw types are unsafe, and it's provided only for compatibility.</p> <p>That is, instead of this:</p> <pre><code>ArrayList peps = new ArrayList(); // BAD!!! No generic safety! </code></pre> <p>you should've used the typesafe generic declaration like this:</p> <pre><code>List&lt;People&gt; peps = new ArrayList&lt;People&gt;(); // GOOD!!! </code></pre> <p>You will then find that <strong>your code doesn't even compile!!</strong> That would be a good thing, because there IS something wrong with the code (<code>Person</code> does not <code>implements Comparable&lt;Person&gt;</code>), <strong>but because you used raw type, the compiler didn't check for this</strong>, and instead you get a <code>ClassCastException</code> at run-time!!!</p> <p>This should convince you to always use typesafe generic types in new code. Always.</p> <h3>See also</h3> <ul> <li><a href="https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it">What is a raw type and why shouldn't we use it?</a></li> </ul>
    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. 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.
    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