Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can create your own <code>Comparator</code> class/object and pass it to <code>Arrays.sort()</code>. Unfortunately, you also need to convert the elements to <code>Character</code>.</p> <p>However, perhaps the most general way is to see each character as a <code>String</code> and use a <code>Collator</code>, as in the following example:</p> <pre><code> // Rules separated in 3 parts only for convenience String rules1= "&lt; A &lt; B &lt; C &lt; D &lt; E &lt; F &lt; G &lt; H &lt; I &lt; J &lt; K &lt; L &lt; M &lt; N &lt; O &lt; P &lt; Q &lt; R &lt; S &lt; T &lt; U &lt; V &lt; W &lt; X &lt; Y &lt; Z" ; String rules2= "&lt; a &lt; b &lt; c &lt; d &lt; e &lt; f &lt; g &lt; h &lt; i &lt; j &lt; k &lt; l &lt; m &lt; n &lt; o &lt; p &lt; q &lt; r &lt; s &lt; t &lt; u &lt; v &lt; w &lt; x &lt; y &lt; z" ; String rules3= "&lt; 0 &lt; 1 &lt; 2 &lt; 3 &lt; 4 &lt; 5 &lt; 6 &lt; 7 &lt; 8 &lt; 9" ; RuleBasedCollator collator= new RuleBasedCollator(rules1+rules2+rules3) ; String input= "h498y948759hrh98A722hjDF94yugerTEr892ur48y" ; // Bulk of the job done here String[] arr= input.split("") ; Arrays.sort(arr,1,arr.length,collator); // Join back in a single string for presentation StringBuilder sb= new StringBuilder() ; for(String e: arr ) sb.append( e ); System.out.println(sb); </code></pre> <p>Output is</p> <pre><code> ADEFTeghhhhjrrrruuyyy222444457788888999999 </code></pre> <p>Changing only collation rules to</p> <pre><code> String rules1= "&lt; 0 &lt; 1 &lt; 2 &lt; 3 &lt; 4 &lt; 5 &lt; 6 &lt; 7 &lt; 8 &lt; 9" ; String rules2= "&lt; A,a &lt; B,b &lt; C,c &lt; D,d &lt; E,e &lt; F,f &lt; G,g &lt; H,h &lt; I,i &lt; J,j &lt; K,k &lt; L,l &lt; M,m &lt; N,n &lt; O,o &lt; P,p &lt; Q,q &lt; R,r &lt; S,s &lt; T,t &lt; U,u &lt; V,v &lt; W,w &lt; X,x &lt; Y,y &lt; Z,z" ; RuleBasedCollator collator= new RuleBasedCollator(rules1+rules2) ; </code></pre> <p>Output is</p> <pre><code> 222444457788888999999ADEeFghhhhjrrrrTuuyyy </code></pre> <p>The main advantage of <code>Collator</code>s is that they allow to sort multi-character Strings according to their internal rules. In fact, this is their main use case.</p> <p>Pretty powerful, eh? (Yes, I'm Canadian in case you didn't guess :-) )</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. 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