diff --git a/packages/lib/services/rest/routes/search.ts b/packages/lib/services/rest/routes/search.ts index fae4fe07a..6bc60a4df 100644 --- a/packages/lib/services/rest/routes/search.ts +++ b/packages/lib/services/rest/routes/search.ts @@ -37,5 +37,8 @@ export default async function(request: Request) { results = (await SearchEngineUtils.notesForQuery(query, false, options)).notes; } - return collectionToPaginatedResults(modelType, results, request); + // We do not sort the results because the search engine has already sorted them in order of + // relevance. + // https://github.com/laurent22/joplin/issues/10088 + return collectionToPaginatedResults(modelType, results, request, { sort: false }); } diff --git a/packages/lib/services/rest/utils/collectionToPaginatedResults.ts b/packages/lib/services/rest/utils/collectionToPaginatedResults.ts index 333cf94e9..299095965 100644 --- a/packages/lib/services/rest/utils/collectionToPaginatedResults.ts +++ b/packages/lib/services/rest/utils/collectionToPaginatedResults.ts @@ -5,13 +5,22 @@ import { Request } from '../Api'; import requestPaginationOptions from '../utils/requestPaginationOptions'; import requestFields from './requestFields'; +export interface Options { + sort?: boolean; +} + // Note that this is inefficient pagination as it requires all the items to // have been loaded first, even if not all of them are returned. // // It's however convenient for smaller lists as it reduces the need for // building complex SQL queries. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied -export default function(itemType: ModelType, items: any[], request: Request): ModelFeedPage { +export default function(itemType: ModelType, items: any[], request: Request, options: Options = null): ModelFeedPage { + options = { + sort: true, + ...options, + }; + const fields = requestFields(request, itemType); const pagination = requestPaginationOptions(request); const startIndex = (pagination.page - 1) * pagination.limit; @@ -33,23 +42,25 @@ export default function(itemType: ModelType, items: any[], request: Request): Mo return newItem; }); - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - sortedItems.sort((a: any, b: any) => { - let v1 = a && (sortBy in a) ? a[sortBy] : ''; - let v2 = b && (sortBy in b) ? b[sortBy] : ''; - if (caseInsensitive && typeof v1 === 'string') v1 = v1.toLowerCase(); - if (caseInsensitive && typeof v2 === 'string') v2 = v2.toLowerCase(); + if (options.sort) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied + sortedItems.sort((a: any, b: any) => { + let v1 = a && (sortBy in a) ? a[sortBy] : ''; + let v2 = b && (sortBy in b) ? b[sortBy] : ''; + if (caseInsensitive && typeof v1 === 'string') v1 = v1.toLowerCase(); + if (caseInsensitive && typeof v2 === 'string') v2 = v2.toLowerCase(); - if (sortDir === PaginationOrderDir.ASC) { - if (v1 < v2) return -1; - if (v2 > v1) return +1; - } else { - if (v1 > v2) return -1; - if (v2 < v1) return +1; - } + if (sortDir === PaginationOrderDir.ASC) { + if (v1 < v2) return -1; + if (v2 > v1) return +1; + } else { + if (v1 > v2) return -1; + if (v2 < v1) return +1; + } - return 0; - }); + return 0; + }); + } return { items: sortedItems.slice(startIndex, startIndex + itemCount),