From 101925bb8aa198c4c4b48f8e1b27048c42b8c5b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20Andr=C3=A9=20R=C3=B8nsen?= Date: Thu, 9 Apr 2026 05:15:55 +0200 Subject: [PATCH] fix(SnippetStore): honour stored order when no search query is active MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit filtered(by:) always sorted snippets by useCount, making manual drag-reordering via move(from:to:) invisible — the backing array was saved correctly but the view immediately re-rendered in useCount order. Now filtered(by:) returns snippets in their stored order when the query is empty, so drag-reordering is preserved. When a search query is active results are still sorted by useCount so the most-used matches surface first. --- Sources/SnippetStore.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Sources/SnippetStore.swift b/Sources/SnippetStore.swift index b2405f0..858d447 100644 --- a/Sources/SnippetStore.swift +++ b/Sources/SnippetStore.swift @@ -113,13 +113,17 @@ class SnippetStore: ObservableObject { } func filtered(by query: String) -> [Snippet] { - let sorted = snippets.sorted { $0.useCount > $1.useCount } - guard !query.isEmpty else { return sorted } + // When there is no search query, honour the stored order so that + // manual drag-reordering (via move(from:to:)) is visible to the user. + // When filtering, sort by useCount so the most-used matches rise first. + guard !query.isEmpty else { return snippets } let q = query.lowercased() - return sorted.filter { - $0.title.lowercased().contains(q) || - $0.value.lowercased().contains(q) - } + return snippets + .filter { + $0.title.lowercased().contains(q) || + $0.value.lowercased().contains(q) + } + .sorted { $0.useCount > $1.useCount } } func move(from source: IndexSet, to destination: Int) {