CloudCannon / pagefind

Static low-bandwidth search at scale
https://pagefind.app
MIT License
3.24k stars 99 forks source link

Sorting in the Default UI #508

Closed searls closed 3 months ago

searls commented 7 months ago

Since filtering is supported in the Default UI, I was wondering if the maintainers are opening to adding support for sorting in the Default UI as well? I was surprised that to simply sort results in reverse-chronological order I would need to throw out the Default UI and roll my own, for instance.

The interface I'd like is:

window.addEventListener('DOMContentLoaded', (event) => {
  new PagefindUI({
    element: "#search",
    resetStyles: false,
    sort: { date: "desc" }
  });
});

I imagine this would entail forwarding this option along all the way to this line, much in the same way filters is.

bglw commented 7 months ago

Yes, this is an easy enough option to pass through! I'll wire that up.

searls commented 7 months ago

That'd be incredible, thank you!On Nov 22, 2023, at 20:55, Liam Bigelow @.***> wrote: Yes, this is an easy enough option to pass through! I'll wire that up.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

bglw commented 7 months ago

Hey @searls — this is available for you to start using in a 1.0.5-rc0 release of Pagefind 🙂

https://unreleased.pagefind.app/docs/ui/#sort

searls commented 7 months ago

Awesome, thank you!

searls commented 7 months ago

I just tested this out and it's not working for me.

My markup includes date metadata and sort attributes:

<time pubdate="" datetime="2010-12-07" data-pagefind-meta="date:2010-12-07" data-pagefind-sort="date:2010-12-07" class="block mb-2 text-sm font-semibold text-center text-secondary">
      Tuesday, Dec 7, 2010
</time>

My JS:

  window.pageFindUi = new PagefindUI({
    element: "#search",
    resetStyles: false,
    autofocus: true,
    sort: { date: "desc" }
  });

But when I search, results are out-of-order:

Screenshot 2023-11-29 at 9 15 31 AM

I deployed this so you can see a live version: justin.searls.co/search

bglw commented 7 months ago

🤦 This is why I publish prereleases! My bad — sorry about that.

v1.0.5-rc1 is out now 🙂

searls commented 7 months ago

Fixed! Looks good.

Also, you'll want to remove this line from the sorting docs:

Sorting is currently only supported when interacting with the Pagefind JS API directly.
volodymyrprokopyuk commented 5 months ago

I hope that the official release v1.0.5 that includes sorting in the Default UI will happen soon. This feature is much needed. I use the standalone binary of pagefind from GitHub. Thank you!

Will the default UI support sorting by multiple keys? E. g. sort: { score: "desc", title: "asc" } meaning sort by score first and then in each group of elements with the same score sort by title.

bglw commented 3 months ago

Sorry for the delay all! This has now landed in the v1.1.0 release :-)

brouberol commented 2 months ago

I'm just dropping by to say thank you! I've been hoping for this feature for years, and it's working like a charm <3