Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a tutorial about ordering objects:</p> <ul> <li><a href="http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html" rel="noreferrer">The Java Tutorials - Collections - Object Ordering</a></li> </ul> <p>Although I will give some examples, I would recommend to read it anyway.</p> <hr> <p>There are various way to sort an <code>ArrayList</code>. If you want to define a <strong>natural</strong> (default) <strong>ordering</strong>, then you need to let <code>Contact</code> implement <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html" rel="noreferrer"><code>Comparable</code></a>. Assuming that you want to sort by default on <code>name</code>, then do (nullchecks omitted for simplicity):</p> <pre><code>public class Contact implements Comparable&lt;Contact&gt; { private String name; private String phone; private Address address; public int compareTo(Contact other) { return name.compareTo(other.name); } // Add/generate getters/setters and other boilerplate. } </code></pre> <p>so that you can just do</p> <pre><code>List&lt;Contact&gt; contacts = new ArrayList&lt;Contact&gt;(); // Fill it. Collections.sort(contacts); </code></pre> <hr> <p>If you want to define an <strong>external controllable ordering</strong> (which overrides the natural ordering), then you need to create a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html" rel="noreferrer"><code>Comparator</code></a>:</p> <pre><code>List&lt;Contact&gt; contacts = new ArrayList&lt;Contact&gt;(); // Fill it. // Now sort by address instead of name (default). Collections.sort(contacts, new Comparator&lt;Contact&gt;() { public int compare(Contact one, Contact other) { return one.getAddress().compareTo(other.getAddress()); } }); </code></pre> <hr> <p>You can even define the <code>Comparator</code>s in the <code>Contact</code> itself so that you can reuse them instead of recreating them everytime:</p> <pre><code>public class Contact { private String name; private String phone; private Address address; // ... public static Comparator&lt;Contact&gt; COMPARE_BY_PHONE = new Comparator&lt;Contact&gt;() { public int compare(Contact one, Contact other) { return one.phone.compareTo(other.phone); } }; public static Comparator&lt;Contact&gt; COMPARE_BY_ADDRESS = new Comparator&lt;Contact&gt;() { public int compare(Contact one, Contact other) { return one.address.compareTo(other.address); } }; } </code></pre> <p>which can be used as follows:</p> <pre><code>List&lt;Contact&gt; contacts = new ArrayList&lt;Contact&gt;(); // Fill it. // Sort by address. Collections.sort(contacts, Contact.COMPARE_BY_ADDRESS); // Sort later by phone. Collections.sort(contacts, Contact.COMPARE_BY_PHONE); </code></pre> <hr> <p>And to cream the top off, you could consider to use a <strong>generic javabean comparator</strong>:</p> <pre><code>public class BeanComparator implements Comparator&lt;Object&gt; { private String getter; public BeanComparator(String field) { this.getter = "get" + field.substring(0, 1).toUpperCase() + field.substring(1); } public int compare(Object o1, Object o2) { try { if (o1 != null &amp;&amp; o2 != null) { o1 = o1.getClass().getMethod(getter, new Class[0]).invoke(o1, new Object[0]); o2 = o2.getClass().getMethod(getter, new Class[0]).invoke(o2, new Object[0]); } } catch (Exception e) { // If this exception occurs, then it is usually a fault of the developer. throw new RuntimeException("Cannot compare " + o1 + " with " + o2 + " on " + getter, e); } return (o1 == null) ? -1 : ((o2 == null) ? 1 : ((Comparable&lt;Object&gt;) o1).compareTo(o2)); } } </code></pre> <p>which you can use as follows:</p> <pre><code>// Sort on "phone" field of the Contact bean. Collections.sort(contacts, new BeanComparator("phone")); </code></pre> <p><em>(as you see in the code, possibly null fields are already covered to avoid NPE's during sort)</em></p>
 

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