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

36
LINES

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

JavaX fragment (include)

static betterCIComparator_es_C betterCIComparator_es_instance;

static betterCIComparator_es_C betterCIComparator_es() {
  if (betterCIComparator_es_instance == null)
    betterCIComparator_es_instance = new betterCIComparator_es_C;
  ret betterCIComparator_es_instance;
}

sclass betterCIComparator_es_C implements Comparator<ES> {
  public int compare(ES e1, ES e2) {
    S s1 = getVar(e1), s2 = getVar(e2);
    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;
  }
}

Author comment

Began life as a copy of #1014136

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1023202
Snippet name: betterCIComparator_es (compared to String.CASE_INSENSITIVE_ORDER, allows nulls)
Eternal ID of this version: #1023202/1
Text MD5: 88a23a4be22f9e503933831443a603a3
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-05-16 19:54:25
Source code size: 1145 bytes / 36 lines
Pitched / IR pitched: No / No
Views / Downloads: 188 / 229
Referenced in: [show references]