diff --git a/src/Core/Services/SearchService.cs b/src/Core/Services/SearchService.cs index 09610942e..8a5b6ac7d 100644 --- a/src/Core/Services/SearchService.cs +++ b/src/Core/Services/SearchService.cs @@ -74,28 +74,34 @@ namespace Bit.Core.Services CancellationToken ct = default, bool deleted = false) { ct.ThrowIfCancellationRequested(); + var matchedCiphers = new List(); + var lowPriorityMatchedCiphers = new List(); query = query.Trim().ToLower(); - return ciphers.Where(c => + + foreach (var c in ciphers) { ct.ThrowIfCancellationRequested(); if (c.Name?.ToLower().Contains(query) ?? false) { - return true; + matchedCiphers.Add(c); } - if (query.Length >= 8 && c.Id.StartsWith(query)) + else if (query.Length >= 8 && c.Id.StartsWith(query)) { - return true; + lowPriorityMatchedCiphers.Add(c); } - if (c.SubTitle?.ToLower().Contains(query) ?? false) + else if (c.SubTitle?.ToLower().Contains(query) ?? false) { - return true; + lowPriorityMatchedCiphers.Add(c); } - if (c.Login?.Uri?.ToLower()?.Contains(query) ?? false) + else if (c.Login?.Uri?.ToLower()?.Contains(query) ?? false) { - return true; + lowPriorityMatchedCiphers.Add(c); } - return false; - }).ToList(); + } + + ct.ThrowIfCancellationRequested(); + matchedCiphers.AddRange(lowPriorityMatchedCiphers); + return matchedCiphers; } public async Task> SearchSendsAsync(string query, Func filter = null, @@ -133,25 +139,31 @@ namespace Bit.Core.Services public List SearchSendsBasic(List sends, string query, CancellationToken ct = default, bool deleted = false) { + var matchedSends = new List(); + var lowPriorityMatchSends = new List(); ct.ThrowIfCancellationRequested(); query = query.Trim().ToLower(); - return sends.Where(s => + + foreach (var s in sends) { ct.ThrowIfCancellationRequested(); if (s.Name?.ToLower().Contains(query) ?? false) { - return true; + matchedSends.Add(s); } - if (s.Text?.Text?.ToLower().Contains(query) ?? false) + else if (s.Text?.Text?.ToLower().Contains(query) ?? false) { - return true; + lowPriorityMatchSends.Add(s); } - if (s.File?.FileName?.ToLower()?.Contains(query) ?? false) + else if (s.File?.FileName?.ToLower()?.Contains(query) ?? false) { - return true; + lowPriorityMatchSends.Add(s); } - return false; - }).ToList(); + } + + ct.ThrowIfCancellationRequested(); + matchedSends.AddRange(lowPriorityMatchSends); + return matchedSends; } } }