Skip to content

Commit

Permalink
♻️ refactor: improve result sort logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhijie0327 authored Dec 30, 2024
1 parent e504e09 commit 7743dd8
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/app/api/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,20 @@ export async function POST(req: NextRequest) {

const results = await response.json();

let searchResults = results.results.sort((a: any, b: any) => b.score - a.score)
if (time_range) {
searchResults = searchResults.sort((a, b) => {
const dateA = new Date(a.publishedDate || 0).getTime();
const dateB = new Date(b.publishedDate || 0).getTime();
return dateB - dateA;
});
}
searchResults = searchResults.slice(0, max_results);
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;
})
.slice(0, max_results);
console.log('Search Results:', searchResults);

return NextResponse.json(searchResults);
Expand Down

0 comments on commit 7743dd8

Please sign in to comment.