Skip to content

Commit

Permalink
try to fix leading 0 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
plrigaux committed Apr 28, 2016
1 parent 1acb2b8 commit 25768e4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 38 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ jar {
repositories { mavenCentral() }

dependencies {

compile 'org.slf4j:slf4j-api:1.7.+'
//compile 'org.slf4j:slf4j-simple:1.7.+'
compile 'ch.qos.logback:logback-classic:1.1.+'


testCompile 'org.hamcrest:java-hamcrest:2.0.0.0'

// We use the latest groovy 2.x version for Spock testing
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/com/plr/comparator/alphanum/NaturalComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NaturalComparator implements Comparator<String> {

private static final Logger logger = LoggerFactory.getLogger(NaturalComparator.class);

final private Comparator<String> alphaComparator;

// TODO make the regex not capture the point
// final static String NUM_PAT =
// "(?:\\s)*((?:^|\\s)[-])?0*([1-9]\\d*|0)((\\.\\d++)(?!\\.\\d))?";
final static String NUM_PAT = "\\s+|((?:(?<=^|\\s)[-])?0*(?:[1-9]\\d*|0))((\\.\\d++)(?!\\.\\d))?";
final static String NUM_PAT = "\\s+|(?:(?:(?<=^|\\s)[-])?0*([1-9]\\d*|0))((\\.\\d++)(?!\\.\\d))?";

final static public Comparator<String> ASCII = new Comparator<String>() {
@Override
Expand Down Expand Up @@ -71,15 +76,17 @@ List<TokenComparable> split(String toSplit) {

int start = 0;

// int j = 0;
int j = 0;
while (matcher.find()) {
// System.out.println("toSplit:'" + toSplit + "'");
// for (int i = 0; i < matcher.groupCount(); i++) {
// System.out.println("find " + j + " Gr" + i + ":'" +
// matcher.group(i) + "'");
// }
// j++;
// System.out.println();

if (logger.isDebugEnabled()) {
logger.debug("toSplit:'{}'", toSplit);
for (int i = 0; i < matcher.groupCount(); i++) {
logger.debug("find {} Gr{}: '{}'", j, i, matcher.group(i));
}
j++;
logger.debug("");
}

if (start != matcher.start()) {
String prev = toSplit.substring(start, matcher.start());
Expand All @@ -99,15 +106,12 @@ List<TokenComparable> split(String toSplit) {

if ('-' == ch) {
isNegative = true;
// skip the '-'
number = number.substring(1);
} else {
isNegative = false;
}


String decimal = matcher.group(2);
//TODO try to find why there is a dot at front
// TODO try to find why there is a dot at front
decimal = decimal == null ? null : decimal.substring(1);

list.add(new NumberTokenComparable(isNegative, number, decimal, wholeStr, alphaComparator));
Expand All @@ -127,8 +131,8 @@ public int compare(String s1, String s2) {

List<TokenComparable> list1 = split(s1);
List<TokenComparable> list2 = split(s2);
// System.out.println(list1);
// System.out.println(list2);
// System.out.println(list1);
// System.out.println(list2);

int len1 = list1.size();
int len2 = list2.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ private int comapreNum(String s1, String s2, NumberTokenComparable other) {

result = comapreDecimals(other);

if (result != 0) {
// if (result != 0) {
return result;
}
// }

//The one that have less leading zeros or space is the smallest
String otherStr = other.str;

len1 = str.length();
len2 = otherStr.length();

result = len1 - len2;
// String otherStr = other.str;
//
// len1 = str.length();
// len2 = otherStr.length();
//
// result = len1 - len2;
}

return result;
Expand Down
21 changes: 21 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>

<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>


<logger name="com.plr.comparator.alphanum" level="INFO" />

<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class NaturalComparatorTest extends Specification {
"-123" | "-0.1"
"-123" | "-1.1"
"-0.99" | "-0.15"
"-1.1532456" | "-0.99"
}


Expand Down Expand Up @@ -346,6 +347,14 @@ class NaturalComparatorTest extends Specification {
println "Sorted: " + list
println "Expected: " + expected



list.eachWithIndex { item, index ->
def exp = expected[index]
boolean b = item == exp
println "$b $index '$item' ----- $exp"
}

list == expected
}

Expand Down Expand Up @@ -406,8 +415,10 @@ class NaturalComparatorTest extends Specification {
println "Sorted: " + list
println "Expected: " + expected

for(int i = 0; i < list.size(); i++) {
list[i] == expected[i]
list.eachWithIndex { item, index ->
def exp = expected[index]
boolean b = item == exp
println "$b $index '$item' ----- $exp"
}

list == expected
Expand Down Expand Up @@ -439,19 +450,22 @@ class NaturalComparatorTest extends Specification {
where:

first | second | comparison
// "doc20.doc" | "doc10.doc" | GREATER
// "doc10.doc" | "doc20.doc" | LESS
// "doc2.doc" | "doc10.doc" | LESS
// "doc2.1.doc" | "doc2.2.doc" | LESS
// "doc2.10.doc" | "doc2.2.doc" | LESS
// "20" | "10" | GREATER
// "2" | "10" | LESS
// "-20" | "10" | LESS
// "-20" | "-10" | LESS
// "pic05" | "pic 5" | GREATER
// "pic02000" | "pic2" | GREATER
// "1-2" | "1-02" | LESS
"Allegia 50 Clasteron" | "Allegia 50B Clasteron" | LESS
"doc20.doc" | "doc10.doc" | GREATER
"doc10.doc" | "doc20.doc" | LESS
"doc2.doc" | "doc10.doc" | LESS
"doc2.1.doc" | "doc2.2.doc" | LESS
"doc2.10.doc" | "doc2.2.doc" | LESS
"20" | "10" | GREATER
"2" | "10" | LESS
"-20" | "10" | LESS
"-20" | "-10" | LESS
"pic05" | "pic 5" | GREATER
"pic02000" | "pic2" | GREATER
"1-2" | "1-02" | LESS
"Allegia 50 Clasteron" | "Allegia 50B Clasteron" | LESS
"z02.doc" | "z1.doc" | GREATER
"pic01" | "pic2" | LESS
"pic05" | "pic 5 something" | LESS
}

def "Mutiple cases2"() {
Expand Down

0 comments on commit 25768e4

Please sign in to comment.