Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Create a <code>Comparator</code> for the job:</p> <pre><code>public class EmployeeComparator implements Comparator&lt;Employee&gt; { private final String type; public EmployeeComparator (String type) { this.type = type; } public int compare(Employee e1, Employee e2) { if (type.equals("name")) { return e1.getName().compareTo(e2.getName()); } return e1.getId().compareTo(e2.getId()); } } </code></pre> <p>Then to use it</p> <pre><code>String type = "name"; // determined at runtime Collections.sort(list, new EmployeeComparator(type)); </code></pre> <p>The reflective version would be similar, except you would look for a method on the object of "get" + type (capitalised) and invoke that and hard cast it to Comparable and use compareTo (I'll try to show the code, but I'm using my iPhone and its a bit of a stretch, but here goes)</p> <pre><code>public class DynamicComparator implements Comparator&lt;Object&gt; { private final String type; // pass in type capitalised, eg "Name" // ie the getter method name minus the "get" public DynamicComparator (String type) { this.type = type; } public int compare(Object o1, Object o2) { // try-catch omitted Method m = o1.getClass().getMethod("get" + type); String s1 = (String)m.invoke(o1); String s2 = (String)m.invoke(o2); return s1.compareTo(s2); } } </code></pre> <hr> <p>OK... Here's how to do it <em>without</em> creating a class, using an anonymous class (with exception handling so code compiles):</p> <pre><code>List&lt;?&gt; list; final String attribute = "Name"; // for example. Also, this is case-sensitive Collections.sort(list, new Comparator&lt;Object&gt;() { public int compare(Object o1, Object o2) { try { Method m = o1.getClass().getMethod("get" + attribute); // Assume String type. If different, you must handle each type String s1 = (String) m.invoke(o1); String s2 = (String) m.invoke(o2); return s1.compareTo(s2); // simply re-throw checked exceptions wrapped in an unchecked exception } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } }); </code></pre>
    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