[vassalengine-svn-trunk] [vassalengine-svn] SF.net SVN: vassalengine:[8464] ASL-src/trunk
Brought to you by:
rodneykinney,
uckelman
From: <uck...@us...> - 2012-12-08 20:01:27
|
Revision: 8464 http://vassalengine.svn.sourceforge.net/vassalengine/?rev=8464&view=rev Author: uckelman Date: 2012-12-08 20:01:19 +0000 (Sat, 08 Dec 2012) Log Message: ----------- Merged uckelman-3.2@8278,8303. Fixed Bug 4312: ASLBoardPicker uses a broken Comparator for sorting boards Modified Paths: -------------- ASL-src/trunk/src/VASL/build/module/map/ASLBoardPicker.java Property Changed: ---------------- ASL-src/trunk/ Property changes on: ASL-src/trunk ___________________________________________________________________ Added: svn:mergeinfo + /ASL-src/branches/uckelman-3.2:8278,8303 Modified: ASL-src/trunk/src/VASL/build/module/map/ASLBoardPicker.java =================================================================== --- ASL-src/trunk/src/VASL/build/module/map/ASLBoardPicker.java 2012-12-08 19:53:33 UTC (rev 8463) +++ ASL-src/trunk/src/VASL/build/module/map/ASLBoardPicker.java 2012-12-08 20:01:19 UTC (rev 8464) @@ -37,6 +37,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.math.BigInteger; import java.text.Collator; import java.util.ArrayList; import java.util.Collection; @@ -48,6 +49,8 @@ import java.util.List; import java.util.StringTokenizer; import java.util.Vector; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.swing.Box; import javax.swing.BoxLayout; @@ -250,17 +253,82 @@ } } } - final Comparator alpha = Collator.getInstance(); + + // + // * Strings with leading zeros sort ahead of those without. + // * Strings with leading integer parts sort ahead of those without. + // * Strings with lesser leading integer parts sort ahead of those with + // greater leading integer parts. + // * Strings which are otherwise equal are sorted lexicographically by + // their trailing noninteger parts. + // + + final Comparator<Object> alpha = Collator.getInstance(); + final Pattern pat = Pattern.compile("((0*)\\d*)(.*)"); + Comparator<String> comp = new Comparator<String>() { public int compare(String o1, String o2) { - try { - return Integer.parseInt(o1) - Integer.parseInt(o2); + final Matcher m1 = pat.matcher(o1); + final Matcher m2 = pat.matcher(o2); + + if (!m1.matches()) { + // impossible + throw new IllegalStateException(); } - catch (NumberFormatException ex) { + + if (!m2.matches()) { + // impossible + throw new IllegalStateException(); } - return alpha.compare(o1, o2); - } + + // count leading zeros + final int z1 = m1.group(2).length(); + final int z2 = m2.group(2).length(); + + // more leading zeros comes first + if (z1 < z2) { + return 1; + } + else if (z1 > z2) { + return -1; + } + + // same number of leading zeros + final String o1IntStr = m1.group(1); + final String o2IntStr = m2.group(1); + if (o1IntStr.length() > 0) { + if (o2IntStr.length() > 0) { + try { + // both strings have integer parts + final BigInteger o1Int = new BigInteger(o1IntStr); + final BigInteger o2Int = new BigInteger(o2IntStr); + + if (!o1Int.equals(o2Int)) { + // one integer part is smaller than the other + return o1Int.compareTo(o2Int); + + } + } + catch (NumberFormatException e) { + // impossible + throw new IllegalStateException(e); + } + } + else { + // only o1 has an integer part + return -1; + } + } + else if (o2IntStr.length() > 0) { + // only o2 has an integer part + return 1; + } + + // the traling string part is decisive + return alpha.compare(m1.group(3), m2.group(3)); + } }; + Collections.sort(sorted, comp); possibleBoards.clear(); for (int i = 0; i < sorted.size(); ++i) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial Remotely access PCs and mobile devices and provide instant support Improve your efficiency, and focus on delivering more value-add services Discover what IT Professionals Know. Rescue delivers http://p.sf.net/sfu/logmein_12329d2d _______________________________________________ vassalengine-svn mailing list vas...@li... https://lists.sourceforge.net/lists/listinfo/vassalengine-svn |