Skip to content

Commit c11f8fc

Browse files
LoayGhreebcalixtus
andauthored
Fix main table sorting (#12371)
* Fix main table sorting * Add changelog entries --------- Co-authored-by: Carl Christian Snethlage <[email protected]>
1 parent 0294bef commit c11f8fc

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
1818
### Fixed
1919

2020
- We fixed an issue where a bib file with UFF-8 charset was wrongly loaded with a different charset [forum#5369](https://discourse.jabref.org/t/jabref-5-15-opens-bib-files-with-shift-jis-encoding-instead-of-utf-8/5369/)
21+
- We fixed an issue where new entries were inserted in the middle of the table instead of at the end. [#12371](https://github.com/JabRef/jabref/pull/12371)
22+
- We fixed an issue where removing the sort from the table did not restore the original order. [#12371](https://github.com/JabRef/jabref/pull/12371)
2123

2224
### Removed
2325

src/main/java/org/jabref/gui/maintable/MainTable.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,28 @@ public MainTable(MainTableDataModel model,
185185
this.setItems(model.getEntriesFilteredAndSorted());
186186

187187
// Enable sorting
188-
model.getEntriesFilteredAndSorted().comparatorProperty().bind(this.comparatorProperty());
188+
// Workaround for a JavaFX bug: https://bugs.openjdk.org/browse/JDK-8301761 (The sorting of the SortedList can become invalid)
189+
// The default comparator of the SortedList does not consider the insertion index of entries that are equal according to the comparator.
190+
// When two entries are equal based on the comparator, the entry that was inserted first should be considered smaller.
191+
this.setSortPolicy(_ -> true);
192+
model.getEntriesFilteredAndSorted().comparatorProperty().bind(
193+
this.comparatorProperty().map(comparator -> {
194+
if (comparator == null) {
195+
return null;
196+
}
197+
198+
return (entry1, entry2) -> {
199+
int result = comparator.compare(entry1, entry2);
200+
if (result != 0) {
201+
return result;
202+
}
203+
// If the entries are equal according to the comparator, compare them by their index in the database.
204+
// The comparison should ideally be based on the database index, but retrieving the index takes log(n). See {@link BibDatabase#indexOf}.
205+
// Using the entry ID is also valid since IDs are monotonically increasing.
206+
return entry1.getEntry().getId().compareTo(entry2.getEntry().getId());
207+
};
208+
})
209+
);
189210

190211
// Store visual state
191212
new PersistenceVisualStateTable(this, mainTablePreferences.getColumnPreferences()).addListeners();

0 commit comments

Comments
 (0)