Make sorting case insensitive

This commit is contained in:
Vishesh Handa
2021-01-12 15:49:44 +01:00
parent 6bf7aa6165
commit eb2f4519a6
2 changed files with 32 additions and 3 deletions

View File

@ -187,13 +187,17 @@ int _sortTitleAsc(Note a, Note b) {
return -1; return -1;
} }
if (!aTitleExists && !bTitleExists) { if (!aTitleExists && !bTitleExists) {
return a.fileName.compareTo(b.fileName); return _sortFileNameAsc(a, b);
} }
return a.title.compareTo(b.title); var aTitle = a.title.toLowerCase();
var bTitle = b.title.toLowerCase();
return aTitle.compareTo(bTitle);
} }
int _sortFileNameAsc(Note a, Note b) { int _sortFileNameAsc(Note a, Note b) {
return a.fileName.compareTo(b.fileName); var aFileName = a.fileName.toLowerCase();
var bFileName = b.fileName.toLowerCase();
return aFileName.compareTo(bFileName);
} }
NoteSortingFunction _reverse(NoteSortingFunction func) { NoteSortingFunction _reverse(NoteSortingFunction func) {

View File

@ -56,5 +56,30 @@ void main() {
expect(notes[2], n3); expect(notes[2], n3);
expect(notes[3], n4); expect(notes[3], n4);
}); });
test('Title', () async {
var folder = NotesFolderFS(null, '/tmp/', Settings(''));
var n1 = Note(folder, '/tmp/1.md');
n1.title = "alpha";
var n2 = Note(folder, '/tmp/2.md');
n2.title = "beta";
var n3 = Note(folder, '/tmp/3.md');
n3.title = "Axios";
var n4 = Note(folder, '/tmp/4.md');
n4.title = "";
var notes = [n1, n2, n3, n4];
var sortFn = SortingMode(SortingField.Title, SortingOrder.Ascending)
.sortingFunction();
notes.sort(sortFn);
expect(notes[0], n1);
expect(notes[1], n3);
expect(notes[2], n2);
expect(notes[3], n4);
});
}); });
} }