Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a generic way to do it with any class without having to implement Comparable on the class you're sorting or create a custom comparator. I've found instances where I don't want to override compareTo because it serves a different purpose, you can't for enums anyways, and constantly creating wrapper classes is a pain. You can pass in a function that outputs a Comparable object you'd like to use for sorting purposes.</p> <p>The toComparable function is only called once per element in the list (not so for a custom comparator), so it's especially good if that call is expensive for some class. Null values are handled internally, so it's easier to use than a custom comparator. One call to Java 7's TimSort algorithm is drastically more efficient than doing a bunch of O(log N) inserts to a SortedMap (red-black tree or other balanced tree implementation). And you aren't restricted to any particular class or interface.</p> <p>Real world performance increases are significant in many cases. For example, the performance increase is around 5x as fast as using a comparator when sorting Doubles using toString() on a list of size 100k.</p> <pre><code>import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.ListIterator; public class GenericLetterSorter { public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"); private final String description; Letter() { description = toString(); } Letter(String description) { this.description = description; } public String getDescription() { return description; } } public static void main(String[] args) { List&lt;Letter&gt; list = new ArrayList&lt;&gt;(Arrays.asList(Letter.values())); sort(list, new ToComparable&lt;Letter&gt;() { @Override public Comparable toComparable(Letter letter) { // sort based on the letter's description return letter == null ? null : letter.getDescription(); } }); for (Letter letter : list) System.out.println(letter == null ? null : letter.name()); } public interface ToComparable&lt;T, C extends Comparable&lt;? super C&gt;&gt; { C toComparable(T t); } public static &lt;T, C extends Comparable&lt;? super C&gt;&gt; void sort(List&lt;T&gt; list, ToComparable&lt;T, C&gt; function) { class Pair implements Comparable&lt;Pair&gt; { final T original; final C comparable; Pair(T original, C comparable) { this.original = original; this.comparable = comparable; } @Override public int compareTo(Pair other) { return comparable == null &amp;&amp; other.comparable == null ? 0 : comparable == null ? -1 : other.comparable == null ? 1 : comparable.compareTo(other.comparable); } } List&lt;Pair&gt; pairs = new ArrayList&lt;&gt;(list.size()); for (T original : list) pairs.add(new Pair(original, function.toComparable(original))); Collections.sort(pairs); ListIterator&lt;T&gt; iter = list.listIterator(); for (Pair pair : pairs) { iter.next(); iter.set(pair.original); } } } </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. This table or related slice is empty.
    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