Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sorting in frontend #95

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions frontend/src/lib/CrateList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,27 @@

let filtered_crates: FullCrate[];

function compare(a: string | number, b: string | number): number {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}

$: filtered_crates = crates
.filter((_, i) => filter.includes(i))
.sort((a, b) => {
.toSorted((a, b) => {
if ($sort_by == "all_downloads") {
return b.downloads - a.downloads;
return compare(b.downloads, a.downloads);
} else if ($sort_by == "recent_downloads") {
return b.this_version_downloads - a.this_version_downloads;
return compare(b.this_version_downloads, a.this_version_downloads);
} else if ($sort_by == "newly_added") {
return b.created_at > a.created_at ? 1 : 0;
return compare(b.created_at, a.created_at);
} else if ($sort_by == "recently_updated") {
return b.updated_at > a.updated_at ? 1 : 0;
return compare(b.updated_at, a.updated_at);
}

// Sort by text search score
Expand All @@ -42,7 +52,7 @@
}
}

return b.name < a.name ? 1 : 0;
return compare(a.name, b.name);
});
</script>

Expand Down