From 39ba1c837441987d3259d04f3d83486a7f5ef7fa Mon Sep 17 00:00:00 2001 From: Zhijie He Date: Mon, 30 Dec 2024 15:47:52 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20improve=20resu?= =?UTF-8?q?lt=20sorting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/search/route.ts | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/app/api/search/route.ts b/src/app/api/search/route.ts index fe87f48..e8682d2 100644 --- a/src/app/api/search/route.ts +++ b/src/app/api/search/route.ts @@ -55,18 +55,11 @@ export async function POST(req: NextRequest) { const results = await response.json(); const searchResults = results.results - .sort((a: any, b: any) => { - const scoreDifference = b.score - a.score; - if (scoreDifference !== 0) return scoreDifference; - - if (time_range) { - const dateA = new Date(a.publishedDate || 0).getTime(); - const dateB = new Date(b.publishedDate || 0).getTime(); - return dateB - dateA; - } - - return 0; - }) + .sort((a: any, b: any) => + b.score !== a.score + ? b.score - a.score + : new Date(b.publishedDate || 0).getTime() - new Date(a.publishedDate || 0).getTime() + ) .slice(0, max_results); console.log('Search Results:', searchResults);