-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sorting custom entry fields that contain numerical values (#6426)
Co-authored-by: Mootez <mootez.saad@medtech.tn>
- Loading branch information
1 parent
7cb7ea6
commit 5be42b3
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
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
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
36 changes: 36 additions & 0 deletions
36
src/main/java/org/jabref/gui/util/comparator/NumericFieldComparator.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/java/org/jabref/gui/util/comparator/NumericFieldComparatorTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.jabref.gui.util.comparator; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class NumericFieldComparatorTest { | ||
|
||
private final NumericFieldComparator comparator = new NumericFieldComparator(); | ||
|
||
@Test | ||
public void compareTwoNumericInputs() { | ||
assertEquals(2, comparator.compare("4", "2")); | ||
} | ||
|
||
@Test | ||
public void compareTwoNullInputs() { | ||
assertEquals(0, comparator.compare(null, null)); | ||
} | ||
|
||
@Test | ||
public void compareTwoInputsWithFirstNull() { | ||
assertEquals(-1, comparator.compare(null, "2")); | ||
} | ||
|
||
@Test | ||
public void compareTwoInputsWithSecondNull() { | ||
assertEquals(1, comparator.compare("4", null)); | ||
} | ||
|
||
@Test | ||
public void compareTwoNotNumericInputs() { | ||
assertEquals(-32, comparator.compare("HELLO", "hello")); | ||
} | ||
|
||
@Test | ||
public void compareStringWithInteger() { | ||
assertEquals(-1, comparator.compare("hi", "2")); | ||
} | ||
|
||
@Test | ||
public void compareIntegerWithString() { | ||
assertEquals(1, comparator.compare("4", "hi")); | ||
} | ||
} |