Skip to content

Commit 17c6280

Browse files
authored
fix: sort tag results (#721)
* sort search results * ruff fix
1 parent ac6774e commit 17c6280

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

tagstudio/src/core/library/alchemy/library.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ def search_library(
632632

633633
def search_tags(
634634
self,
635-
name: str,
635+
name: str | None,
636636
) -> list[Tag]:
637637
"""Return a list of Tag records matching the query."""
638638
tag_limit = 100

tagstudio/src/qt/modals/tag_search.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,28 @@ def on_tag_modal_saved():
186186

187187
def update_tags(self, query: str | None = None):
188188
logger.info("[Tag Search Super Class] Updating Tags")
189-
190189
# TODO: Look at recycling rather than deleting and re-initializing
191190
while self.scroll_layout.count():
192191
self.scroll_layout.takeAt(0).widget().deleteLater()
193-
194192
tag_results = self.lib.search_tags(name=query)
195193
if len(tag_results) > 0:
196-
self.first_tag_id = tag_results[0].id
197-
else:
198-
self.first_tag_id = None
199-
200-
for tag in tag_results:
201-
if tag.id not in self.exclude:
194+
results_1 = []
195+
results_2 = []
196+
for tag in tag_results:
197+
if tag.id in self.exclude:
198+
continue
199+
elif query and tag.name.lower().startswith(query.lower()):
200+
results_1.append(tag)
201+
else:
202+
results_2.append(tag)
203+
results_1.sort(key=lambda tag: len(tag.name))
204+
results_2.sort()
205+
for tag in results_1 + results_2:
202206
self.scroll_layout.addWidget(self.__build_row_item_widget(tag))
203-
204-
# If query doesnt exist add create button
205-
if len(tag_results) == 0:
207+
else:
208+
# If query doesnt exist add create button
206209
c = self.construct_tag_button(query)
207210
self.scroll_layout.addWidget(c)
208-
209211
self.search_field.setFocus()
210212

211213
def on_return(self, text: str):

0 commit comments

Comments
 (0)