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

115
LINES

< > BotCompany Repo | #1012843 // David Koelle's AlphanumComparator

JavaX fragment (include)

1  
/*
2  
 * The Alphanum Algorithm is an improved sorting algorithm for strings
3  
 * containing numbers.  Instead of sorting numbers in ASCII order like
4  
 * a standard sort, this algorithm sorts numbers in numeric order.
5  
 *
6  
 * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
7  
 *
8  
 * Released under the MIT License - https://opensource.org/licenses/MIT
9  
 *
10  
 * Copyright 2007-2017 David Koelle
11  
 *
12  
 * Permission is hereby granted, free of charge, to any person obtaining
13  
 * a copy of this software and associated documentation files (the "Software"),
14  
 * to deal in the Software without restriction, including without limitation
15  
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  
 * and/or sell copies of the Software, and to permit persons to whom the
17  
 * Software is furnished to do so, subject to the following conditions:
18  
 *
19  
 * The above copyright notice and this permission notice shall be included
20  
 * in all copies or substantial portions of the Software.
21  
 *
22  
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26  
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
27  
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
28  
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
29  
 */
30  
31  
/**
32  
 * This is an updated version with enhancements made by Daniel Migowski,
33  
 * Andre Bogus, and David Koelle. Updated by David Koelle in 2017.
34  
 */
35  
 
36  
sclass AlphanumComparator implements Comparator<S> {
37  
  bool ignoreCase;
38  
  
39  
  private final boolean isDigit(char ch) {
40  
    return ((ch >= 48) && (ch <= 57));
41  
  }
42  
43  
  /** Length of string is passed in for improved efficiency (only need to calculate it once) **/
44  
  private final String getChunk(String s, int slength, int marker) {
45  
    StringBuilder chunk = new StringBuilder();
46  
    char c = s.charAt(marker);
47  
    chunk.append(c);
48  
    marker++;
49  
    if (isDigit(c))
50  
        while (marker < slength) {
51  
            c = s.charAt(marker);
52  
            if (!isDigit(c))
53  
                break;
54  
            chunk.append(c);
55  
            marker++;
56  
        }
57  
    else
58  
        while (marker < slength) {
59  
            c = s.charAt(marker);
60  
            if (isDigit(c))
61  
                break;
62  
            chunk.append(c);
63  
            marker++;
64  
        }
65  
    return chunk.toString();
66  
  }
67  
68  
  public int compare(String s1, String s2) {
69  
  	if (s1 == null) ret s2 == null ? 0 : -1;
70  
    if (s2 == null) ret 1;
71  
72  
    int thisMarker = 0;
73  
    int thatMarker = 0;
74  
    int s1Length = s1.length();
75  
    int s2Length = s2.length();
76  
77  
    while (thisMarker < s1Length && thatMarker < s2Length) {
78  
      String thisChunk = getChunk(s1, s1Length, thisMarker);
79  
      thisMarker += thisChunk.length();
80  
81  
      String thatChunk = getChunk(s2, s2Length, thatMarker);
82  
      thatMarker += thatChunk.length();
83  
84  
      // If both chunks contain numeric characters, sort them numerically
85  
      int result = 0;
86  
      if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0)))
87  
      {
88  
          // Simple chunk comparison by length.
89  
          int thisChunkLength = thisChunk.length();
90  
          result = thisChunkLength - thatChunk.length();
91  
          // If equal, the first different number counts
92  
          if (result == 0)
93  
          {
94  
              for (int i = 0; i < thisChunkLength; i++)
95  
              {
96  
                  result = thisChunk.charAt(i) - thatChunk.charAt(i);
97  
                  if (result != 0)
98  
                  {
99  
                      return result;
100  
                  }
101  
              }
102  
          }
103  
      } 
104  
      else
105  
      {
106  
          result = thisChunk.compareTo(thatChunk);
107  
      }
108  
109  
      if (result != 0)
110  
          return result;
111  
    }
112  
    
113  
    ret s1Length - s2Length;
114  
  }
115  
}

Author comment

http://www.davekoelle.com/alphanum.html

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, qsqiayxyrbia, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1012843
Snippet name: David Koelle's AlphanumComparator
Eternal ID of this version: #1012843/6
Text MD5: 42deedfe6a94aa7cd28f8fcbffb0385b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-12-27 21:18:59
Source code size: 3995 bytes / 115 lines
Pitched / IR pitched: No / No
Views / Downloads: 377 / 1058
Version history: 5 change(s)
Referenced in: [show references]