source: josm/trunk/src/org/openstreetmap/josm/tools/AlphanumComparator.java@ 6615

Last change on this file since 6615 was 6126, checked in by bastiK, 11 years ago

applied #8953 - sorting by name relations with accented letters (patch by windu.2b)

File size: 4.3 KB
Line 
1package org.openstreetmap.josm.tools;
2
3/*
4 * The Alphanum Algorithm is an improved sorting algorithm for strings
5 * containing numbers. Instead of sorting numbers in ASCII order like a standard
6 * sort, this algorithm sorts numbers in numeric order.
7 *
8 * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
9 *
10 *
11 * This library is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 */
25import java.text.Collator;
26import java.util.Comparator;
27import java.util.Locale;
28
29/**
30 * The Alphanum Algorithm is an improved sorting algorithm for strings
31 * containing numbers: Instead of sorting numbers in ASCII order like a standard
32 * sort, this algorithm sorts numbers in numeric order.
33 *
34 * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
35 *
36 * This is an updated version with enhancements made by Daniel Migowski, Andre
37 * Bogus, and David Koelle and others.
38 *
39 */
40public class AlphanumComparator implements Comparator<String> {
41
42 /**
43 * Length of string is passed in for improved efficiency (only need to
44 * calculate it once) *
45 */
46 private String getChunk(String s, int slength, int marker) {
47 StringBuilder chunk = new StringBuilder();
48 char c = s.charAt(marker);
49 chunk.append(c);
50 marker++;
51 if (Character.isDigit(c)) {
52 while (marker < slength) {
53 c = s.charAt(marker);
54 if (!Character.isDigit(c)) {
55 break;
56 }
57 chunk.append(c);
58 marker++;
59 }
60 } else {
61 while (marker < slength) {
62 c = s.charAt(marker);
63 if (Character.isDigit(c)) {
64 break;
65 }
66 chunk.append(c);
67 marker++;
68 }
69 }
70 return chunk.toString();
71 }
72
73 @Override
74 public int compare(String s1, String s2) {
75 if (s1 == null && s2 == null) {
76 return 0;
77 } else if (s1 == null) {
78 return -1;
79 } else if (s2 == null) {
80 return 1;
81 }
82
83 int thisMarker = 0;
84 int thatMarker = 0;
85 int s1Length = s1.length();
86 int s2Length = s2.length();
87
88 while (thisMarker < s1Length && thatMarker < s2Length) {
89 String thisChunk = getChunk(s1, s1Length, thisMarker);
90 thisMarker += thisChunk.length();
91
92 String thatChunk = getChunk(s2, s2Length, thatMarker);
93 thatMarker += thatChunk.length();
94
95 // If both chunks contain numeric characters, sort them numerically
96 int result;
97 if (Character.isDigit(thisChunk.charAt(0)) && Character.isDigit(thatChunk.charAt(0))) {
98 // Simple chunk comparison by length.
99 int thisChunkLength = thisChunk.length();
100 result = thisChunkLength - thatChunk.length();
101 // If equal, the first different number counts
102 if (result == 0) {
103 for (int i = 0; i < thisChunkLength; i++) {
104 result = thisChunk.charAt(i) - thatChunk.charAt(i);
105 if (result != 0) {
106 return result;
107 }
108 }
109 }
110 } else {
111 // Instantiate the collator
112 Collator compareOperator = Collator.getInstance(Locale.getDefault());
113 // Compare regardless of accented letters
114 compareOperator.setStrength(Collator.SECONDARY);
115 result = compareOperator.compare(thisChunk, thatChunk);
116 }
117
118 if (result != 0) {
119 return result;
120 }
121 }
122
123 return s1Length - s2Length;
124 }
125}
Note: See TracBrowser for help on using the repository browser.