Created
September 19, 2013 23:56
-
-
Save Fingel/6631499 to your computer and use it in GitHub Desktop.
A comparator for String representing numbers, where 1.10 is greater than 1.1 and 1.2, like a table of contents.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static class TocComparator implements Comparator<String>{ | |
public int compare(String s1, String s2){ | |
Integer s1front = Integer.parseInt(s1.substring(0, s1.indexOf('.'))); | |
Integer s2front = Integer.parseInt(s2.substring(0, s2.indexOf('.'))); | |
if(s1front.equals(s2front)){ | |
Integer f1ass = Integer.parseInt(s1.substring(s1.indexOf('.')+1)); | |
Integer f2ass = Integer.parseInt(s2.substring(s2.indexOf('.')+1)); | |
return f1ass.compareTo(f2ass); | |
} | |
else{ | |
return s1front.compareTo(s2front); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment