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).

1  
static betterCIComparator_C betterCIComparator_instance;
2  
3  
static betterCIComparator_C betterCIComparator() {
4  
  if (betterCIComparator_instance == null)
5  
    betterCIComparator_instance = new betterCIComparator_C;
6  
  ret betterCIComparator_instance;
7  
}
8  
9  
final sclass betterCIComparator_C implements Comparator<S> {
10  
  public int compare(S s1, S s2) {
11  
    if (s1 == null) ret s2 == null ? 0 : -1;
12  
    if (s2 == null) ret 1;
13  
  
14  
    int n1 = s1.length();
15  
    int n2 = s2.length();
16  
    int min = Math.min(n1, n2);
17  
    for (int i = 0; i < min; i++) {
18  
        char c1 = s1.charAt(i);
19  
        char c2 = s2.charAt(i);
20  
        if (c1 != c2) {
21  
            c1 = Character.toUpperCase(c1);
22  
            c2 = Character.toUpperCase(c2);
23  
            if (c1 != c2) {
24  
                c1 = Character.toLowerCase(c1);
25  
                c2 = Character.toLowerCase(c2);
26  
                if (c1 != c2) {
27  
                    // No overflow because of numeric promotion
28  
                    return c1 - c2;
29  
                }
30  
            }
31  
        }
32  
    }
33  
    return n1 - n2;
34  
  }
35  
}

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: 361 / 626
Version history: 2 change(s)
Referenced in: [show references]