Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>class ArrayComparator implements Comparator&lt;Comparable[]&gt; { private final int columnToSort; private final boolean ascending; public ArrayComparator(int columnToSort, boolean ascending) { this.columnToSort = columnToSort; this.ascending = ascending; } public int compare(Comparable[] c1, Comparable[] c2) { int cmp = c1[columnToSort].compareTo(c2[columnToSort]); return ascending ? cmp : -cmp; } } </code></pre> <p>This way you can handle any type of data in those arrays (as long as they're Comparable) and you can sort any column in ascending or descending order.</p> <pre><code>String[][] data = getData(); Arrays.sort(data, new ArrayComparator(0, true)); </code></pre> <p>PS: make sure you check for <code>ArrayIndexOutOfBounds</code> and others.</p> <p>EDIT: The above solution would only be helpful if you are able to actually store a <code>java.util.Date</code> in the first column <em>or if your date format allows you to use plain String comparison for those values</em>. Otherwise, you need to convert that String to a Date, and you can achieve that using a callback interface (as a general solution). Here's an enhanced version:</p> <pre><code>class ArrayComparator implements Comparator&lt;Object[]&gt; { private static Converter DEFAULT_CONVERTER = new Converter() { @Override public Comparable convert(Object o) { // simply assume the object is Comparable return (Comparable) o; } }; private final int columnToSort; private final boolean ascending; private final Converter converter; public ArrayComparator(int columnToSort, boolean ascending) { this(columnToSort, ascending, DEFAULT_CONVERTER); } public ArrayComparator(int columnToSort, boolean ascending, Converter converter) { this.columnToSort = columnToSort; this.ascending = ascending; this.converter = converter; } public int compare(Object[] o1, Object[] o2) { Comparable c1 = converter.convert(o1[columnToSort]); Comparable c2 = converter.convert(o2[columnToSort]); int cmp = c1.compareTo(c2); return ascending ? cmp : -cmp; } } interface Converter { Comparable convert(Object o); } class DateConverter implements Converter { private static final DateFormat df = new SimpleDateFormat("yyyy.MM.dd hh:mm"); @Override public Comparable convert(Object o) { try { return df.parse(o.toString()); } catch (ParseException e) { throw new IllegalArgumentException(e); } } } </code></pre> <p>And at this point, you can sort on your first column with:</p> <pre><code>Arrays.sort(data, new ArrayComparator(0, true, new DateConverter()); </code></pre> <p>I skipped the checks for nulls and other error handling issues.</p> <p><strong>I agree this is starting to look like a framework already. :)</strong></p> <p><em>Last (hopefully) edit: I only now realize that your date format allows you to use plain String comparison. If that is the case, you don't need the "enhanced version".</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