Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to change the ordering of the list you can use the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort" rel="nofollow noreferrer">Collections.sort()</a> method that takes a <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html" rel="nofollow noreferrer">comparator</a> as an argument.</p> <p>Based on your code you seem to have some confusion between the <code>Comparable</code> and <code>Comparator</code> interfaces. A class that implements the <code>Comparable</code> interface means that it has some sort of natural ordering (e.g. the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html" rel="nofollow noreferrer">Integer</a> class is naturally ordered based on the int it represents). Using a comparator you can order objects based on some other ordering criteria.</p> <p>From the code you have posted, I would like to ask whether you actually intend to order the ElencoEsami class or if you were intending to order the EsameMedico objects?</p> <p>If its the EsameMedio class that should be ordered, then its the EsameMedico class that should implement the Comparable interface. Assuming that the comparison you do in the addEsame() method is the natural ordering of the EsameMedico class, you can implement the compareTo() method in EsameMedico like this:</p> <pre><code>public class EsameMedico implements Comparable&lt;EsameMedico&gt;{ ... public int compareTo(Object o) { EsameMedico e = (EsameMedico)o; if(getData().after(e.getData())) { return 1; } else if(e.getData().after(getData())){ return -1; } else { return 0; } } </code></pre> <p>}</p> <p>You can then change your addEsame() method to simply insert into the list and call Collections.sort() after each insertion. A better approach of course, would be to use a <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeSet.html" rel="nofollow noreferrer">TreeSet</a> rather than an ArrayList. If you did this each EsameMedico would be inserted according to its natural order and you wouldn't have to perform any sorting yourself.</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. This table or related slice is empty.
    1. 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