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
;
26 import java
.util
.Comparator
;
28 import com
.owncloud
.android
.datamodel
.OCFile
;
31 * This is an updated version with enhancements made by Daniel Migowski,
32 * Andre Bogus, and David Koelle
34 * To convert to use Templates (Java 1.5+):
35 * - Change "implements Comparator" to "implements Comparator<String>"
36 * - Change "compare(Object o1, Object o2)" to "compare(String s1, String s2)"
37 * - Remove the type checking and casting in compare().
40 * Use the static "sort" method from the java.util.Collections class:
41 * Collections.sort(your list, new AlphanumComparator());
43 public class AlphanumComparator
implements Comparator
<OCFile
>
45 private final boolean isDigit(char ch
)
47 return ch
>= 48 && ch
<= 57;
50 /** Length of string is passed in for improved efficiency (only need to calculate it once) **/
51 private final String
getChunk(String s
, int slength
, int marker
)
53 StringBuilder chunk
= new StringBuilder();
54 char c
= s
.charAt(marker
);
59 while (marker
< slength
)
69 while (marker
< slength
)
78 return chunk
.toString();
81 public int compare(OCFile o1
, OCFile o2
)
83 String s1
= (String
)o1
.getRemotePath().toLowerCase();
84 String s2
= (String
)o2
.getRemotePath().toLowerCase();
88 int s1Length
= s1
.length();
89 int s2Length
= s2
.length();
91 while (thisMarker
< s1Length
&& thatMarker
< s2Length
)
93 String thisChunk
= getChunk(s1
, s1Length
, thisMarker
);
94 thisMarker
+= thisChunk
.length();
96 String thatChunk
= getChunk(s2
, s2Length
, thatMarker
);
97 thatMarker
+= thatChunk
.length();
99 // If both chunks contain numeric characters, sort them numerically
101 if (isDigit(thisChunk
.charAt(0)) && isDigit(thatChunk
.charAt(0)))
103 // Simple chunk comparison by length.
104 int thisChunkLength
= thisChunk
.length();
105 result
= thisChunkLength
- thatChunk
.length();
106 // If equal, the first different number counts
109 for (int i
= 0; i
< thisChunkLength
; i
++)
111 result
= thisChunk
.charAt(i
) - thatChunk
.charAt(i
);
120 result
= thisChunk
.compareTo(thatChunk
);
127 return s1Length
- s2Length
;