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

Last change on this file since 6898 was 6804, checked in by simon04, 10 years ago

fix #9668 - Sort entries in validation results naturally

File size: 4.7 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 private static final AlphanumComparator INSTANCE = new AlphanumComparator();
43
44 public static AlphanumComparator getInstance() {
45 return INSTANCE;
46 }
47
48 /**
49 * Constructs a new Alphanum Comparator.
50 * @deprecated use {@link #getInstance()} instead.
51 */
52 @Deprecated
53 public AlphanumComparator() {
54 }
55
56 /**
57 * Length of string is passed in for improved efficiency (only need to
58 * calculate it once) *
59 */
60 private String getChunk(String s, int slength, int marker) {
61 StringBuilder chunk = new StringBuilder();
62 char c = s.charAt(marker);
63 chunk.append(c);
64 marker++;
65 if (Character.isDigit(c)) {
66 while (marker < slength) {
67 c = s.charAt(marker);
68 if (!Character.isDigit(c)) {
69 break;
70 }
71 chunk.append(c);
72 marker++;
73 }
74 } else {
75 while (marker < slength) {
76 c = s.charAt(marker);
77 if (Character.isDigit(c)) {
78 break;
79 }
80 chunk.append(c);
81 marker++;
82 }
83 }
84 return chunk.toString();
85 }
86
87 @Override
88 public int compare(String s1, String s2) {
89 if (s1 == null && s2 == null) {
90 return 0;
91 } else if (s1 == null) {
92 return -1;
93 } else if (s2 == null) {
94 return 1;
95 }
96
97 int thisMarker = 0;
98 int thatMarker = 0;
99 int s1Length = s1.length();
100 int s2Length = s2.length();
101
102 while (thisMarker < s1Length && thatMarker < s2Length) {
103 String thisChunk = getChunk(s1, s1Length, thisMarker);
104 thisMarker += thisChunk.length();
105
106 String thatChunk = getChunk(s2, s2Length, thatMarker);
107 thatMarker += thatChunk.length();
108
109 // If both chunks contain numeric characters, sort them numerically
110 int result;
111 if (Character.isDigit(thisChunk.charAt(0)) && Character.isDigit(thatChunk.charAt(0))) {
112 // Simple chunk comparison by length.
113 int thisChunkLength = thisChunk.length();
114 result = thisChunkLength - thatChunk.length();
115 // If equal, the first different number counts
116 if (result == 0) {
117 for (int i = 0; i < thisChunkLength; i++) {
118 result = thisChunk.charAt(i) - thatChunk.charAt(i);
119 if (result != 0) {
120 return result;
121 }
122 }
123 }
124 } else {
125 // Instantiate the collator
126 Collator compareOperator = Collator.getInstance(Locale.getDefault());
127 // Compare regardless of accented letters
128 compareOperator.setStrength(Collator.SECONDARY);
129 result = compareOperator.compare(thisChunk, thatChunk);
130 }
131
132 if (result != 0) {
133 return result;
134 }
135 }
136
137 return s1Length - s2Length;
138 }
139}
Note: See TracBrowser for help on using the repository browser.