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 com
.owncloud
.android
.datamodel
;
26 import java
.util
.Comparator
;
29 * This is an updated version with enhancements made by Daniel Migowski,
30 * Andre Bogus, and David Koelle
32 * To convert to use Templates (Java 1.5+):
33 * - Change "implements Comparator" to "implements Comparator<String>"
34 * - Change "compare(Object o1, Object o2)" to "compare(String s1, String s2)"
35 * - Remove the type checking and casting in compare().
38 * Use the static "sort" method from the java.util.Collections class:
39 * Collections.sort(your list, new AlphanumComparator());
41 public class AlphanumComparator
implements Comparator
<OCFile
>
43 private final boolean isDigit(char ch
)
45 return ch
>= 48 && ch
<= 57;
48 /** Length of string is passed in for improved efficiency (only need to calculate it once) **/
49 private final String
getChunk(String s
, int slength
, int marker
)
51 StringBuilder chunk
= new StringBuilder();
52 char c
= s
.charAt(marker
);
57 while (marker
< slength
)
67 while (marker
< slength
)
76 return chunk
.toString();
79 public int compare(OCFile o1
, OCFile o2
)
81 String s1
= (String
)o1
.getRemotePath().toLowerCase();
82 String s2
= (String
)o2
.getRemotePath().toLowerCase();
86 int s1Length
= s1
.length();
87 int s2Length
= s2
.length();
89 while (thisMarker
< s1Length
&& thatMarker
< s2Length
)
91 String thisChunk
= getChunk(s1
, s1Length
, thisMarker
);
92 thisMarker
+= thisChunk
.length();
94 String thatChunk
= getChunk(s2
, s2Length
, thatMarker
);
95 thatMarker
+= thatChunk
.length();
97 // If both chunks contain numeric characters, sort them numerically
99 if (isDigit(thisChunk
.charAt(0)) && isDigit(thatChunk
.charAt(0)))
101 // Simple chunk comparison by length.
102 int thisChunkLength
= thisChunk
.length();
103 result
= thisChunkLength
- thatChunk
.length();
104 // If equal, the first different number counts
107 for (int i
= 0; i
< thisChunkLength
; i
++)
109 result
= thisChunk
.charAt(i
) - thatChunk
.charAt(i
);
118 result
= thisChunk
.compareTo(thatChunk
);
125 return s1Length
- s2Length
;