Skip to content

Commit 20f2fae

Browse files
committed
sorting function for recipes
1 parent edf47bf commit 20f2fae

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/routes/category/[slug]/+page.svelte

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@
33
export let data;
44
let { recipes, slug } = data;
55
$: ({ recipes, slug } = data);
6-
import { faArrowLeft } from "@fortawesome/free-solid-svg-icons";
6+
import { faArrowLeft, faChevronDown } from "@fortawesome/free-solid-svg-icons";
77
import Icon from "svelte-awesome/components/Icon.svelte";
88
import Recipe from "../../../components/Recipe.svelte";
99
import Back from "../../../components/Back.svelte";
1010
import RatingScale from "../../../components/RatingScale.svelte";
11+
12+
let sortBy = 'title';
13+
14+
$: sortedRecipes = [...recipes].sort((a, b) => {
15+
switch (sortBy) {
16+
case 'title':
17+
return a.metadata.title.localeCompare(b.metadata.title);
18+
case 'rating':
19+
return b.metadata.rating - a.metadata.rating;
20+
default:
21+
return 0;
22+
}
23+
});
1124
</script>
1225

1326
<svelte:head>
@@ -18,9 +31,17 @@
1831

1932
<RatingScale />
2033

34+
<div class="mb-4">
35+
<label for="sort-select" class="block text-md font-medium">Sort by:</label>
36+
<select id="sort-select" bind:value={sortBy} class="px-3 py-2 border border-zinc-700 rounded-md bg-zinc-700 text-white focus:outline-zinc-500">
37+
<option value="title">Title (A-Z)</option>
38+
<option value="rating">Rating (High to Low)</option>
39+
</select>
40+
</div>
41+
2142
{#if recipes.length > 0}
2243
<div class="grid lg:grid-cols-2 gap-4 mt-4">
23-
{#each recipes as recipe}
44+
{#each sortedRecipes as recipe}
2445
<a
2546
href={`${base}/category/${slug}/recipe/${recipe.slug}`}
2647
class="hover:text-white no-underline"

0 commit comments

Comments
 (0)