Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an alternative - if a component is found to contain less that 3 parts then parts are added at the start to take up the slack. It then uses a sort order array to define which columns should be compared next:</p> <pre><code> public void test() { String[] test = new String[]{ "alpha", "beta", "charlie", "zeta", // Added to demonstrate correctness. "sitea.alpha", "sitea.charlie", "sitea.pagea.beta", "sitea.pageb.beta", "sitea.pagea.charlie", "siteb.alpha", "siteb.delta", "siteb.pagef.alpha", "siteb.pageb.echo", "siteb.pageb.golf", "siteb.pagea.hotel", "siteb.pageb.hotel", "siteb.pagec.hotel" }; Arrays.sort(test); System.out.println("Normal sort: "+Arrays.toString(test)); Arrays.sort(test, new Comparator&lt;String&gt;() { // How many columns to pad to. final int padTo = 3; // What to pad with. final String padWith = ""; // What order to compare the resultant columns in. final int[] order = {0, 2, 1}; @Override public int compare(String s1, String s2) { String[] s1parts = padArray(s1.split(Pattern.quote(".")), padTo, padWith); String[] s2parts = padArray(s2.split(Pattern.quote(".")), padTo, padWith); int diff = 0; for ( int i = 0; diff == 0 &amp;&amp; i &lt; order.length; i++ ) { diff = s1parts[order[i]].compareTo(s2parts[order[i]]); } return diff; } String [] padArray(String[] array, int padTo, String padWith) { String [] padded = new String[padTo]; for ( int i = 0; i &lt; padded.length; i++ ) { padded[padded.length - i - 1] = i &lt; array.length ? array[i]: padWith; } return padded; } }); System.out.println("Special sort: "+Arrays.toString(test)); } </code></pre> <p>prints (more or less):</p> <pre><code>Normal sort: [alpha, beta, charlie, sitea.alpha, sitea.charlie, sitea.pagea.beta, sitea.pagea.charlie, sitea.pageb.beta, siteb.alpha, siteb.delta, siteb.pagea.hotel, siteb.pageb.echo, siteb.pageb.golf, siteb.pageb.hotel, siteb.pagec.hotel, siteb.pagef.alpha, zeta] Special sort: [alpha, beta, charlie, sitea.alpha, sitea.charlie, siteb.alpha, siteb.delta, zeta, siteb.pagef.alpha, sitea.pagea.beta, sitea.pageb.beta, sitea.pagea.charlie, siteb.pageb.echo, siteb.pageb.golf, siteb.pagea.hotel, siteb.pageb.hotel, siteb.pagec.hotel] </code></pre> <p>There does seem to be some ambiguity in your requirements but this code is structured so you can, with trivial tweaks, achieve most interpretations of your comparison quite simply.</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. 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