package org.jabref.gui.util.comparator;

import java.util.Comparator;

/**
 * Comparator for numeric cases.
 * The purpose of this class is to add the numeric comparison, because values are sorted
 * as if they were strings.
 */
public class NumericFieldComparator implements Comparator<String> {

    @Override
    public int compare(String val1, String val2) {
        // We start by implementing the comparison in the edge cases (if one of the values is null)
        if (val1 == null && val2 == null) { return 0; }
        if (val1 == null) { return -1; } // (we assume that "null" is "less than" any other value)
        if (val2 == null) { return 1; }

        // Now we start the conversion to integers.
        Integer valInt1 = null;
        Integer valInt2 = null;
        try {
            valInt1 = Integer.parseInt(val1.trim()); // In case the user added an unnecessary white space (e.g. 1 1 instead of 11)
        } catch (NumberFormatException ignore) { /* do nothing */ }
        try {
            valInt2 = Integer.parseInt(val2.trim());
        } catch (NumberFormatException ignore) { /* do nothing */ }
        if (valInt1 == null && valInt2 == null) { return val1.compareTo(val2); } // None of the values were parsed (i.e both are not numeric)
        // so we will use the normal string comparison.
        if (valInt1 == null) { return -1; } // We assume that strings "are less" than integers
        if (valInt2 == null) { return 1; }

        // If we arrive at this stage then both values are actually numeric !
        return valInt1 - valInt2;
    }
}

@@ -100,19 +100,16 @@
 c cases.
-%0A *
  The pur
@@ -452,16 +452,17 @@
 is null)
+.
 %0A       
@@ -489,32 +489,44 @@
  val2 == null) %7B
+%0A           
  return 0; %7D%0A   
@@ -519,19 +519,28 @@
 eturn 0;
+%0A       
  %7D%0A
+%0A
         
@@ -562,27 +562,25 @@
 l) %7B
- return -1; %7D
+%0A           
  // 
-(w
+W
 e as
@@ -626,17 +626,51 @@
 er value
-)
+.%0A            return -1;%0A        %7D%0A
 %0A       
@@ -681,32 +681,44 @@
 (val2 == null) %7B
+%0A           
  return 1; %7D%0A%0A  
@@ -703,32 +703,40 @@
        return 1;
+%0A       
  %7D%0A%0A        // N
@@ -869,53 +869,17 @@
     
-valInt1 = Integer.parseInt(val1.trim()); // I
+// Trim i
 n ca
@@ -947,16 +947,70 @@
 d of 11)
+.%0A            valInt1 = Integer.parseInt(val1.trim());
 %0A       
@@ -1042,35 +1042,47 @@
 eption ignore) %7B
- /*
+%0A            //
  do nothing */ %7D
@@ -1068,35 +1068,40 @@
    // do nothing
- */
+%0A       
  %7D%0A        try %7B
@@ -1206,11 +1206,23 @@
 e) %7B
- /*
+%0A            //
  do 
@@ -1232,14 +1232,20 @@
 hing
- */
+%0A       
  %7D%0A
+%0A
     
@@ -1293,39 +1293,20 @@
 l) %7B
- return val1.compareTo(val2); %7D
+%0A           
  // 
@@ -1371,16 +1371,20 @@
         
+  
+  
 // so we
@@ -1423,16 +1423,68 @@
 arison.%0A
+            return val1.compareTo(val2);%0A        %7D%0A%0A
         
@@ -1509,21 +1509,20 @@
 l) %7B
- return -1; %7D
+%0A           
  // 
@@ -1569,16 +1569,50 @@
 ntegers%0A
+            return -1;%0A        %7D%0A%0A
         
@@ -1633,16 +1633,28 @@
  null) %7B
+%0A           
  return 
@@ -1655,16 +1655,24 @@
 eturn 1;
+%0A       
  %7D%0A%0A    

Fix checkstyle, moved some comments for better understanding