Skip to content

Commit 50e46ec

Browse files
herzadinataBOCOVO
authored andcommitted
fix(dblm-ext): sorting search dropdown results
1 parent 8fe0954 commit 50e46ec

File tree

1 file changed

+30
-1
lines changed
  • packages/json-table-schema-visualizer/src/components/Search

1 file changed

+30
-1
lines changed

packages/json-table-schema-visualizer/src/components/Search/Search.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,36 @@ const Search = ({ tables }: SearchProps) => {
5959
});
6060
});
6161

62-
return results;
62+
const collator = new Intl.Collator(undefined, {
63+
sensitivity: "base",
64+
numeric: true,
65+
});
66+
67+
const isExact = (r: SearchResult) =>
68+
r.name.toLowerCase() === search.toLowerCase();
69+
70+
const resultsSorted = results.sort((a, b) => {
71+
// 0) exact name match goes to the very top (table or column)
72+
const aExact = isExact(a);
73+
const bExact = isExact(b);
74+
if (aExact !== bExact) return aExact ? -1 : 1;
75+
76+
// 1) put tables before columns
77+
if (a.type !== b.type) return a.type === "table" ? -1 : 1;
78+
79+
// 2) within tables: sort by table name (same as `name`)
80+
if (a.type === "table") {
81+
return collator.compare(a.name, b.name);
82+
}
83+
84+
// 3) within columns: sort by column name, then by table name
85+
const byColName = collator.compare(a.name, b.name);
86+
if (byColName !== 0) return byColName;
87+
88+
return collator.compare(a.tableName, b.tableName);
89+
});
90+
91+
return resultsSorted;
6392
}, [tables, search]);
6493

6594
const handleSelect = (result: SearchResult) => {

0 commit comments

Comments
 (0)