Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

35
LINES

< > BotCompany Repo | #1014136 // betterCIComparator (compared to String.CASE_INSENSITIVE_ORDER, allows nulls)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (60L/1K).

static betterCIComparator_C betterCIComparator_instance;

static betterCIComparator_C betterCIComparator() {
  if (betterCIComparator_instance == null)
    betterCIComparator_instance = new betterCIComparator_C;
  ret betterCIComparator_instance;
}

final sclass betterCIComparator_C implements Comparator<S> {
  public int compare(S s1, S s2) {
    if (s1 == null) ret s2 == null ? 0 : -1;
    if (s2 == null) ret 1;
  
    int n1 = s1.length();
    int n2 = s2.length();
    int min = Math.min(n1, n2);
    for (int i = 0; i < min; i++) {
        char c1 = s1.charAt(i);
        char c2 = s2.charAt(i);
        if (c1 != c2) {
            c1 = Character.toUpperCase(c1);
            c2 = Character.toUpperCase(c2);
            if (c1 != c2) {
                c1 = Character.toLowerCase(c1);
                c2 = Character.toLowerCase(c2);
                if (c1 != c2) {
                    // No overflow because of numeric promotion
                    return c1 - c2;
                }
            }
        }
    }
    return n1 - n2;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1014136
Snippet name: betterCIComparator (compared to String.CASE_INSENSITIVE_ORDER, allows nulls)
Eternal ID of this version: #1014136/3
Text MD5: c22d9f1793e8877260dd4881087008b7
Transpilation MD5: adc3b5723f0cb7ef7270252bf0ce199e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-23 09:32:31
Source code size: 1080 bytes / 35 lines
Pitched / IR pitched: No / No
Views / Downloads: 357 / 621
Version history: 2 change(s)
Referenced in: [show references]