Home / E-commerce and Business / Product Filter and Sort

Product Filter and Sort in JavaScript, Free with Live Demo

Free product filter and sort in plain JavaScript. Filter by category, price, colour, rating and stock, sort by price or rating, see active filter chips and share the URL.

Open live demoDownload HTML fileView code on GitHub
Product Filter and Sort JavaScript project: a product listing with category, price, colour and rating filters saved in the URL

Runs on: URLSearchParams and the History API. Works in all modern browsers.

What is the Product Filter and Sort?

A ceramics shop listing with a filter sidebar: categories with counts, a price range, colour swatches, minimum rating and in stock only, plus five ways to sort.

Active filters appear as chips you can remove one by one. Everything is written to the URL, so a filtered view can be bookmarked or shared.

Good for

  • Online shop category pages
  • Property and car listings
  • Job boards and directories
  • Course and event catalogues

What this project does

How it works

  1. Filter, then sortEach product must pass every active filter. The survivors are sorted with one of five compare functions.
  2. The URL is the stateEvery change writes the filters to the address bar with URLSearchParams. Opening that link restores the same view.
  3. Chips for active filtersEach active filter becomes a small button above the results, and clicking it removes just that filter.

The key JavaScript

This is the heart of the project. The full file has the rest, including the screen layout and error handling.

function run() {
  const s = readFilters();
  const list = products
    .filter((p) => (!s.cats.length || s.cats.includes(p.cat)) && p.price <= s.max && p.rating >= s.rating)
    .sort(sorters[s.sort]);
  const q = new URLSearchParams();
  s.cats.forEach((c) => q.append("cat", c));
  if (s.max !== Infinity) q.set("max", s.max);
  history.replaceState(null, "", "?" + q);
  render(list);
}

How to use it

  1. Click Download HTML file above.
  2. Open the file in a code editor, like VS Code.
  3. Run it from a local server with npx serve . so the camera, microphone and AI features are allowed.
  4. Change the text and colors, then upload it to GitHub Pages, Netlify or your own site. It is one file with no build step.

Questions people ask

Should filtering happen in the browser or on the server?

In the browser is great for up to a few thousand products. For bigger catalogues, send the same URL parameters to your server.

Why store filters in the URL?

Shared links and the back button then show the same results, and search engines can index popular filter pages.

How do the category counts work?

They count products per category from the list. You can update them to reflect the other active filters.

More E-commerce and Business projects