Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Commit

Permalink
Improve entry sorting stability
Browse files Browse the repository at this point in the history
  • Loading branch information
perry-mitchell committed Jun 2, 2023
1 parent 1fb4cfb commit 4c605cd
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/components/vault/utils/entries.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import R from "ramda";
import Fuse from "fuse.js";
import { getFacadeField } from "../../../utils";
import { EntryPropertyType } from "buttercup/web";

const options = {
keys: ["fields.value"],
Expand All @@ -16,13 +16,21 @@ export const filterEntries = (entries = [], term = "") => {
return fuse.search(term).map(hit => ({ ...hit.item, matches: hit.matches }));
};

export const sortEntries = (entries = [], asc = true) => {
const sortByTitleCaseInsensitive = R.sortBy(
R.compose(
R.toLower,
R.compose(R.prop("value"), R.find(R.propEq("property", "title")), R.prop("fields"))
)
);
const sorted = sortByTitleCaseInsensitive(entries);
return asc ? sorted : R.reverse(sorted);
};
export function sortEntries(entries = [], asc = true) {
return entries.sort((a, b) => {
const aTitleProp = a.fields.find(
f => f.property === "title" && f.propertyType === EntryPropertyType.Property
);
const bTitleProp = b.fields.find(
f => f.property === "title" && f.propertyType === EntryPropertyType.Property
);
const aTitle = aTitleProp?.value ?? "";
const bTitle = bTitleProp?.value ?? "";
if (aTitle < bTitle) {
return asc ? -1 : 1;
} else if (aTitle > bTitle) {
return asc ? 1 : -1;
}
return 0;
});
}

0 comments on commit 4c605cd

Please sign in to comment.