Skip to content

Commit

Permalink
Fix main table sorting (#12371)
Browse files Browse the repository at this point in the history
* Fix main table sorting

* Add changelog entries

---------

Co-authored-by: Carl Christian Snethlage <[email protected]>
  • Loading branch information
LoayGhreeb and calixtus authored Jan 9, 2025
1 parent 0294bef commit c11f8fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
### Fixed

- 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/)
- 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)
- 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)

### Removed

Expand Down
23 changes: 22 additions & 1 deletion src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,28 @@ public MainTable(MainTableDataModel model,
this.setItems(model.getEntriesFilteredAndSorted());

// Enable sorting
model.getEntriesFilteredAndSorted().comparatorProperty().bind(this.comparatorProperty());
// Workaround for a JavaFX bug: https://bugs.openjdk.org/browse/JDK-8301761 (The sorting of the SortedList can become invalid)
// The default comparator of the SortedList does not consider the insertion index of entries that are equal according to the comparator.
// When two entries are equal based on the comparator, the entry that was inserted first should be considered smaller.
this.setSortPolicy(_ -> true);
model.getEntriesFilteredAndSorted().comparatorProperty().bind(
this.comparatorProperty().map(comparator -> {
if (comparator == null) {
return null;
}

return (entry1, entry2) -> {
int result = comparator.compare(entry1, entry2);
if (result != 0) {
return result;
}
// If the entries are equal according to the comparator, compare them by their index in the database.
// The comparison should ideally be based on the database index, but retrieving the index takes log(n). See {@link BibDatabase#indexOf}.
// Using the entry ID is also valid since IDs are monotonically increasing.
return entry1.getEntry().getId().compareTo(entry2.getEntry().getId());
};
})
);

// Store visual state
new PersistenceVisualStateTable(this, mainTablePreferences.getColumnPreferences()).addListeners();
Expand Down

0 comments on commit c11f8fc

Please sign in to comment.