Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting custom entry fields that contain numerical values #6426

Merged
merged 9 commits into from
May 8, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where JabRef switched to discrete graphics under macOS [#5935](https://github.com/JabRef/jabref/issues/5935)
- We fixed an issue where the Preferences entry preview will be unexpected modified leads to Value too long exception [#6198](https://github.com/JabRef/jabref/issues/6198)
- We fixed an issue where custom jstyles for Open/LibreOffice would only be valid if a layout line for the entry type `default` was at the end of the layout section [#6303](https://github.com/JabRef/jabref/issues/6303)
- We fixed an issue where sort on numeric cases was broken. [#6349](https://github.com/JabRef/jabref/issues/6349)

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.jabref.gui.specialfields.SpecialFieldsPreferences;
import org.jabref.gui.util.OptionalValueTableCellFactory;
import org.jabref.gui.util.ValueTableCellFactory;
import org.jabref.gui.util.comparator.NumericFieldComparator;
import org.jabref.gui.util.comparator.PriorityFieldComparator;
import org.jabref.gui.util.comparator.RankingFieldComparator;
import org.jabref.gui.util.comparator.ReadStatusFieldComparator;
Expand Down Expand Up @@ -212,6 +213,7 @@ private Node createGroupColorRegion(BibEntryTableViewModel entry, List<AbstractG
new ValueTableCellFactory<BibEntryTableViewModel, String>()
.withText(text -> text)
.install(column);
column.setComparator(new NumericFieldComparator());
column.setSortable(true);
return column;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jabref.gui.util.comparator;

import java.util.Comparator;

/**
* Comparator for numeric cases.
*/
public class NumericFieldComparator implements Comparator<String> {

@Override
public int compare(String val1, String val2) {
/*
* The purpose of this class is to add the numeric comparison, because values are sorted
* as if they were strings.
* */

// 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) {}
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;
}



}