2 * The Alphanum Algorithm is an improved sorting algorithm for strings
\r
3 * containing numbers. Instead of sorting numbers in ASCII order like
\r
4 * a standard sort, this algorithm sorts numbers in numeric order.
\r
6 * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
\r
9 * This library is free software; you can redistribute it and/or
\r
10 * modify it under the terms of the GNU Lesser General Public
\r
11 * License as published by the Free Software Foundation; either
\r
12 * version 2.1 of the License, or any later version.
\r
14 * This library is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
17 * Lesser General Public License for more details.
\r
19 * You should have received a copy of the GNU Lesser General Public
\r
20 * License along with this library; if not, write to the Free Software
\r
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
\r
26 * Subsequently this code had been hacked up to make it genericized and support
\r
27 * folding upper/lower case.
\r
29 package net.sf.openrocket.util;
\r
31 import java.text.Collator;
\r
32 import java.util.Comparator;
\r
35 * This is an updated version with enhancements made by Daniel Migowski,
\r
36 * Andre Bogus, and David Koelle
\r
38 * To convert to use Templates (Java 1.5+):
\r
39 * - Change "implements Comparator" to "implements Comparator<String>"
\r
40 * - Change "compare(Object o1, Object o2)" to "compare(String s1, String s2)"
\r
41 * - Remove the type checking and casting in compare().
\r
43 * To use this class:
\r
44 * Use the static "sort" method from the java.util.Collections class:
\r
45 * Collections.sort(your list, new AlphanumComparator());
\r
47 public class AlphanumComparator implements Comparator<String>
\r
50 private static final Collator sorter = Collator.getInstance();
\r
52 sorter.setStrength(Collator.TERTIARY);
\r
53 sorter.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
\r
56 private final boolean isDigit(char ch)
\r
58 return ch >= 48 && ch <= 57;
\r
61 /** Length of string is passed in for improved efficiency (only need to calculate it once) **/
\r
62 private final String getChunk(String s, int slength, int marker)
\r
64 StringBuilder chunk = new StringBuilder();
\r
65 char c = s.charAt(marker);
\r
70 while (marker < slength)
\r
72 c = s.charAt(marker);
\r
80 while (marker < slength)
\r
82 c = s.charAt(marker);
\r
89 return chunk.toString();
\r
93 public int compare(String s1, String s2)
\r
98 int s1Length = s1.length();
\r
99 int s2Length = s2.length();
\r
101 while (thisMarker < s1Length && thatMarker < s2Length)
\r
103 String thisChunk = getChunk(s1, s1Length, thisMarker);
\r
104 thisMarker += thisChunk.length();
\r
106 String thatChunk = getChunk(s2, s2Length, thatMarker);
\r
107 thatMarker += thatChunk.length();
\r
109 // If both chunks contain numeric characters, sort them numerically
\r
111 if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0)))
\r
113 // Simple chunk comparison by length.
\r
114 int thisChunkLength = thisChunk.length();
\r
115 result = thisChunkLength - thatChunk.length();
\r
116 // If equal, the first different number counts
\r
119 for (int i = 0; i < thisChunkLength; i++)
\r
121 result = thisChunk.charAt(i) - thatChunk.charAt(i);
\r
130 result = sorter.compare(thisChunk, thatChunk);
\r
137 return s1Length - s2Length;
\r