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.
6 * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 package third_parties
.daveKoeller
;
27 import java
.util
.Comparator
;
29 import com
.owncloud
.android
.datamodel
.OCFile
;
32 * This is an updated version with enhancements made by Daniel Migowski,
33 * Andre Bogus, and David Koelle
35 * To convert to use Templates (Java 1.5+):
36 * - Change "implements Comparator" to "implements Comparator<String>"
37 * - Change "compare(Object o1, Object o2)" to "compare(String s1, String s2)"
38 * - Remove the type checking and casting in compare().
41 * Use the static "sort" method from the java.util.Collections class:
42 * Collections.sort(your list, new AlphanumComparator());
44 public class AlphanumComparator
implements Comparator
<OCFile
>
46 private final boolean isDigit(char ch
)
48 return ch
>= 48 && ch
<= 57;
51 /** Length of string is passed in for improved efficiency (only need to calculate it once) **/
52 private final String
getChunk(String s
, int slength
, int marker
)
54 StringBuilder chunk
= new StringBuilder();
55 char c
= s
.charAt(marker
);
60 while (marker
< slength
)
70 while (marker
< slength
)
79 return chunk
.toString();
82 public int compare(OCFile o1
, OCFile o2
){
83 String s1
= o1
.getRemotePath().toLowerCase();
84 String s2
= o2
.getRemotePath().toLowerCase();
86 return compare(s1
, s2
);
89 public int compare(File f1
, File f2
){
90 String s1
= f1
.getPath().toLowerCase();
91 String s2
= f2
.getPath().toLowerCase();
93 return compare(s1
, s2
);
96 public int compare(String s1
, String s2
) {
99 int s1Length
= s1
.length();
100 int s2Length
= s2
.length();
102 while (thisMarker
< s1Length
&& thatMarker
< s2Length
)
104 String thisChunk
= getChunk(s1
, s1Length
, thisMarker
);
105 thisMarker
+= thisChunk
.length();
107 String thatChunk
= getChunk(s2
, s2Length
, thatMarker
);
108 thatMarker
+= thatChunk
.length();
110 // If both chunks contain numeric characters, sort them numerically
112 if (isDigit(thisChunk
.charAt(0)) && isDigit(thatChunk
.charAt(0)))
114 // Simple chunk comparison by length.
115 int thisChunkLength
= thisChunk
.length();
116 result
= thisChunkLength
- thatChunk
.length();
117 // If equal, the first different number counts
120 for (int i
= 0; i
< thisChunkLength
; i
++)
122 result
= thisChunk
.charAt(i
) - thatChunk
.charAt(i
);
131 result
= thisChunk
.compareTo(thatChunk
);
138 return s1Length
- s2Length
;