diff --git a/src/features/browser-assistant/sort.ts b/src/features/browser-assistant/sort.ts index e41ee26..cdb3eed 100644 --- a/src/features/browser-assistant/sort.ts +++ b/src/features/browser-assistant/sort.ts @@ -14,6 +14,20 @@ export function compareText(left: string, right: string) { }); } +function compareOptionalText(left: string | null | undefined, right: string | null | undefined) { + const leftValue = left?.trim() ?? ""; + const rightValue = right?.trim() ?? ""; + + const leftEmpty = leftValue.length === 0; + const rightEmpty = rightValue.length === 0; + + if (leftEmpty && rightEmpty) return 0; + if (leftEmpty) return 1; + if (rightEmpty) return -1; + + return compareText(leftValue, rightValue); +} + export function compareProfileId(left: string, right: string) { const leftKey = profileSortKeyValue(left); const rightKey = profileSortKeyValue(right); @@ -27,7 +41,7 @@ export function sortProfiles(items: ProfileSummary[], sortKey: ProfileSortKey) { return profiles.sort((left, right) => { if (sortKey === "email") { return ( - compareText(left.email ?? "", right.email ?? "") || + compareOptionalText(left.email, right.email) || compareText(left.name, right.name) || compareProfileId(left.id, right.id) ); @@ -53,9 +67,9 @@ export function sortBookmarks(items: BookmarkSummary[], sortKey: BookmarkSortKey const bookmarks = [...items]; return bookmarks.sort((left, right) => { if (sortKey === "url") { - return compareText(left.url, right.url); + return compareOptionalText(left.url, right.url) || compareText(left.title, right.title); } - return compareText(left.title, right.title) || compareText(left.url, right.url); + return compareOptionalText(left.title, right.title) || compareText(left.url, right.url); }); }