Skip to content

Commit edae432

Browse files
Select the entry which has smaller dictonary order when merge (#7708)
1 parent 4282394 commit edae432

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
491491
- We added an option in preferences to allow for integers in field "edition" when running database in bibtex mode. [#4680](https://github.com/JabRef/jabref/issues/4680)
492492
- We added the ability to use negation in export filter layouts. [#5138](https://github.com/JabRef/jabref/pull/5138)
493493
- Focus on Name Area instead of 'OK' button whenever user presses 'Add subgroup'. [#6307](https://github.com/JabRef/jabref/issues/6307)
494+
- We changed the behavior of merging that the entry which has "smaller" bibkey will be selected. [#7395](https://github.com/JabRef/jabref/issues/7395)
494495

495496
### Fixed
496497

src/main/java/org/jabref/gui/mergeentries/MergeEntriesAction.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import org.jabref.gui.undo.NamedCompound;
1414
import org.jabref.gui.undo.UndoableInsertEntries;
1515
import org.jabref.gui.undo.UndoableRemoveEntries;
16+
import org.jabref.logic.bibtex.comparator.EntryComparator;
1617
import org.jabref.logic.l10n.Localization;
1718
import org.jabref.model.database.BibDatabaseContext;
1819
import org.jabref.model.entry.BibEntry;
20+
import org.jabref.model.entry.field.InternalField;
1921

2022
public class MergeEntriesAction extends SimpleCommand {
2123

@@ -52,7 +54,19 @@ public void execute() {
5254
BibEntry one = selectedEntries.get(0);
5355
BibEntry two = selectedEntries.get(1);
5456

55-
MergeEntriesDialog dlg = new MergeEntriesDialog(one, two);
57+
// compare two entries
58+
BibEntry first;
59+
BibEntry second;
60+
EntryComparator entryComparator = new EntryComparator(false, false, InternalField.KEY_FIELD);
61+
if (entryComparator.compare(one, two) <= 0) {
62+
first = one;
63+
second = two;
64+
} else {
65+
first = two;
66+
second = one;
67+
}
68+
69+
MergeEntriesDialog dlg = new MergeEntriesDialog(first, second);
5670
dlg.setTitle(Localization.lang("Merge entries"));
5771
Optional<BibEntry> mergedEntry = dialogService.showCustomDialogAndWait(dlg);
5872
if (mergedEntry.isPresent()) {

src/main/java/org/jabref/logic/bibtex/comparator/EntryComparator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public int compare(BibEntry e1, BibEntry e2) {
7272
// Sort by type.
7373
f1 = e1.getType();
7474
f2 = e2.getType();
75+
} else if (sortField.equals(InternalField.KEY_FIELD)) {
76+
f1 = e1.getCitationKey().orElse(null);
77+
f2 = e2.getCitationKey().orElse(null);
7578
} else if (sortField.isNumeric()) {
7679
try {
7780
Integer i1 = Integer.parseInt((String) f1);

src/test/java/org/jabref/logic/bibtex/comparator/EntryComparatorTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jabref.logic.bibtex.comparator;
22

33
import org.jabref.model.entry.BibEntry;
4+
import org.jabref.model.entry.field.InternalField;
45
import org.jabref.model.entry.field.StandardField;
56

67
import org.junit.jupiter.api.Test;
@@ -69,4 +70,35 @@ void bothEntriesNumericAscending() throws Exception {
6970

7071
assertEquals(-1, entryComparator.compare(entry1, entry2));
7172
}
73+
74+
@Test
75+
void compareObjectsByKeyAscending() {
76+
BibEntry e1 = new BibEntry();
77+
BibEntry e2 = new BibEntry();
78+
e1.setCitationKey("Mayer2019b");
79+
e2.setCitationKey("Mayer2019a");
80+
assertEquals(1, new EntryComparator(false, false, InternalField.KEY_FIELD).compare(e1, e2));
81+
assertEquals(-1, new EntryComparator(false, false, InternalField.KEY_FIELD).compare(e2, e1));
82+
}
83+
84+
@Test
85+
void compareObjectsByKeyWithNull() {
86+
BibEntry e1 = new BibEntry();
87+
BibEntry e2 = new BibEntry();
88+
e1.setCitationKey("Mayer2019b");
89+
assertEquals(-1, new EntryComparator(false, false, InternalField.KEY_FIELD).compare(e1, e2));
90+
assertEquals(1, new EntryComparator(false, false, InternalField.KEY_FIELD).compare(e2, e1));
91+
}
92+
93+
@Test
94+
void compareObjectsByKeyWithBlank() {
95+
BibEntry e1 = new BibEntry();
96+
BibEntry e2 = new BibEntry();
97+
e1.setCitationKey("Mayer2019b");
98+
e2.setCitationKey(" ");
99+
assertEquals(-1, new EntryComparator(false, false, InternalField.KEY_FIELD).compare(e1, e2));
100+
assertEquals(1, new EntryComparator(false, false, InternalField.KEY_FIELD).compare(e2, e1));
101+
}
72102
}
103+
104+

0 commit comments

Comments
 (0)