Skip to content

Commit

Permalink
♻️ refactor: improve result sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhijie0327 authored Dec 30, 2024
1 parent 7743dd8 commit 39ba1c8
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/app/api/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 39ba1c8

Please sign in to comment.