Common Filters And Sorting Bugs and How to Catch Them

Common Filters And Sorting Bugs and How to Catch Them

By · January 16, 2026 · 19 min read · Common Issues

Common Filters And Sorting Bugs and How to Catch Them

Filters and sorting are everyday features in web and mobile applications, yet they hide a surprising number of defects that slip past scripted tests. Users encounter missing results, duplicated entries, or chaotic ordering, which erodes trust and can lead to abandoned checkouts or faulty analytics. This guide walks through the most frequent filter and sorting bug patterns, shows how they manifest, explains why they arise, and gives concrete steps to reproduce, detect, fix, and prevent them. By the end you will have a ready‑to‑use test matrix, a checklist for manual and automated checks, and insight into how persona‑driven autonomous exploration surfaces issues that traditional automation overlooks.

1. Why Filters and Sorting Are Bug Prone

Filters and sorting sit at the intersection of data access, UI state, and business logic. A change in any layer—backend query, API pagination, client‑side state management, or UI component—can break the contract that the displayed list matches the user’s intent. Common root causes include:

Each of these can produce a visible symptom to the user while leaving unit tests green because the tests often hit a happy‑path dataset or mock deterministic responses.

2. Bug Pattern 1 – Off‑by‑One Page Index Errors

What it looks like

A user clicks “Next page” on a paginated table and sees the same items as the previous page, or the last page is completely empty. In mobile apps, infinite scroll may stop prematurely or duplicate the final batch.

Why it happens

Backend APIs often accept page (starting at 1) or offset (starting at 0). If the frontend mixes the two conventions, the calculated offset becomes page * pageSize instead of (page‑1) * pageSize. Conversely, when the backend returns totalCount and the frontend computes totalPages = Math.ceil(totalCount / pageSize), an off‑by‑one in the ceiling calculation can hide the final page when totalCount is an exact multiple of pageSize.

How to reproduce

  1. Set page size to 10.
  2. Insert exactly 30 records into the system.
  3. Navigate to page 3 (should show records 21‑30).
  4. Observe duplication or missing data.

Detection

Fix

Prevention checklist

3. Bug Pattern 2 – Incorrect Comparator Logic (NaN, null handling)

What it looks like

Sorting a column of numbers places NaN or null values at unpredictable positions—sometimes at the top, sometimes at the bottom, sometimes causing the entire list to appear unsorted.

Why it happens

JavaScript’s Array.prototype.sort converts elements to strings unless a compare function is supplied. A naïve compare like return a - b yields NaN when either operand is NaN or null, and the sort algorithm treats NaN as “greater than” any number, leading to erratic ordering. Similar issues appear in SQL (ORDER BY col) where NULLS FIRST/LAST defaults differ between databases.

How to reproduce

  1. Create a list: [5, null, 2, NaN, 9].
  2. Apply the buggy comparator.
  3. Observe that null and NaN end up interleaved or at the front.

Detection

Fix


function safeNumberComparator(a, b) {
  if (a === null && b === null) return 0;
  if (a === null) return 1;   // treat null as larger
  if (b === null) return -1;
  if (Number.isNaN(a) && Number.isNaN(b)) return 0;
  if (Number.isNaN(a)) return 1; // NaN after numbers
  if (Number.isNaN(b)) return -1;
  return a - b;
}

Prevention checklist

4. Bug Pattern 3 – Locale‑Specific Sorting Failures

What it looks like

A list of names sorted alphabetically places “Ångström” after “Z” in a Swedish locale but before “A” in an English locale, causing confusion for users who expect a consistent order across language settings.

Why it happens

Developers often rely on the default JavaScript String.prototype.localeCompare without specifying a locale, which defaults to the runtime environment’s locale. In CI containers the locale may be C, while production servers run en_US.UTF-8. The result is different sort orders between test and production.

How to reproduce

  1. Set environment locale to C.
  2. Sort an array containing ['Ångström', 'Zebra', 'Apple'].
  3. Observe order: ['Zebra', 'Apple', 'Ångström'].
  4. Switch locale to sv_SE and repeat; order changes.

Detection

Fix


function sortByName(a, b, locale = 'en') {
  return a.name.localeCompare(b.name, locale, { sensitivity: 'base' });
}

Prevention checklist

5. Bug Pattern 4 – Stale Cache Leading to Inconsistent Results

What it looks like

After deleting a record, the user still sees it in the filtered list until they manually refresh the page. In some cases, applying a filter after an edit shows outdated counts, causing mistrust in the UI.

Why it happens

Frontend state management libraries (Redux, MobX, React Query) often cache query results to avoid unnecessary network calls. If the cache invalidation logic does not listen to the relevant mutation events (e.g., deleteItem, updateItem), the stale data persists.

How to reproduce

  1. Load a list of 20 items, apply a filter that matches 5 of them.
  2. Delete one of the matching items via a separate API call.
  3. Observe that the filtered list still shows 5 items, and the deleted item is present.
  4. Refresh the page; the list now shows 4 items.

Detection

Fix

Prevention checklist

6. Bug Pattern 5 – Missing Secondary Sort Keys Causing Non‑Deterministic Order

What it looks like

When two records have identical primary sort values (e.g., same timestamp), their order changes each time the list is reloaded, leading to flickering UI and difficulty reproducing bugs.

Why it happens

A sort comparator only considers the primary key. If the comparator returns 0 for equal keys, the underlying sort algorithm may leave the items in whatever order they happened to be in the array, which can vary due to asynchronous data fetching or pagination.

How to reproduce

  1. Insert two records with identical createdAt values (e.g., both at 2024-01-01T12:00:00Z).
  2. Sort by createdAt descending.
  3. Reload the page multiple times; note that the two records swap positions intermittently.

Detection

Fix


function compareItems(a, b) {
  if (a.timestamp !== b.timestamp) return b.timestamp - a.timestamp;
  return a.id.localeCompare(b.id); // assuming string IDs
}

Prevention checklist

7. Bug Pattern 6 – Filter Combination Logic Errors (AND/OR Misuse)

What it looks like

A user selects “Category = Electronics” AND “Price < $100”. The results show items that are either electronics or cheap, effectively treating the criteria as OR. Conversely, a filter meant to show “Sale OR New Arrival” may incorrectly require both conditions, hiding valid items.

Why it happens

When building query objects or SQL WHERE clauses programmatically, developers sometimes mistakenly use || instead of && (or vice versa) when combining predicate functions, or they mishandle nested condition groups. UI state that stores each filter as an independent boolean can also lead to logic errors when the UI combines them incorrectly.

How to reproduce

  1. Create a dataset:
  1. Apply filters: Category = Electronics AND Price < $100.
  2. Expected result: only Item C.
  3. Observe that Item A and B also appear (OR behavior) or that nothing appears (incorrect AND).

Detection

Fix


function passesAllFilters(item, filters) {
  return Object.entries(filters).every(([key, value]) => {
    if (value === null) return true; // no filter
    return item[key] === value;
  });
}

Prevention checklist

8. Bug Pattern 7 – Performance‑Related Sorting Timeouts

What it looks like

On a product listing page with 100 000 items, clicking the “Sort by price” button causes the UI to freeze for several seconds, sometimes triggering a “page unresponsive” warning in the browser.

Why it happens

Client‑side sorting of large arrays blocks the main thread. If the dataset is fetched unsorted from the backend and the frontend attempts to sort it in‑place, the operation can exceed the frame budget (≈16 ms). Similar timeouts occur on the server when an unindexed ORDER BY forces a filesort on millions of rows.

How to reproduce

  1. Load a list with 150 000 records (e.g., via a seed script).
  2. Open the browser’s performance profiler.
  3. Trigger the sort action and record the main‑thread blocking time.
  4. Observe a blocking period > 200 ms.

Detection

Fix

Prevention checklist

9. Bug Pattern 8 – Accessibility‑Related Filter UI Bugs

What it looks like

A screen‑reader user announces that the “Apply Filters” button is unavailable, or keyboard users cannot reach the filter dropdown because it is trapped inside a modal that loses focus.

Why it happens

Developers often build custom filter widgets using <div> elements without proper ARIA roles, states, or keyboard event handlers. When the filter panel is opened via a button, focus is not moved into the panel, and escaping does not return focus to the trigger.

How to reproduce

  1. Navigate to the filter page using only the Tab key.
  2. Attempt to open the filter panel with Enter or Space.
  3. Verify that focus lands inside the panel and that arrow keys can change options.
  4. Close the panel with Escape and confirm focus returns to the button that opened it.
  5. Run a screen‑reader (NVDA, VoiceOver) and listen for announcements of state changes.

Detection

Fix

Prevention checklist

10. Bug Pattern 9 – Race Conditions in Asynchronous Data Fetch

What it looks like

A user types a search query, receives results, then quickly changes the query. The older request resolves after the newer one, causing the UI to flash stale results before showing the correct list.

Why it happens

Multiple fetch or axios calls are initiated without canceling previous ones. When the promises resolve out of order, the UI state is overwritten by the slower response.

How to reproduce

  1. In the browser devtools, throttle network to “Slow 3G”.
  2. Type “a” → wait 500 ms → type “ab”.
  3. Observe that after a brief delay the list shows results for “a” before switching to “ab”.

Detection

Fix

Prevention checklist

11. Bug Pattern 10 – Misaligned Facet Counts After Filtering

What it looks like

After applying a filter, the facet (refinement) counts displayed next to each category no longer sum to the total number of shown items, or they show counts for items that are now excluded by the active filter.

Why it happens

Facet counts are often computed from a separate, unfiltered aggregation query. When the UI updates the product list based on filtered results but forgets to rerun the facet query with the same filters, the counts become stale.

How to reproduce

  1. On an e‑commerce catalog, select “Brand = X”.
  2. Observe that the facet for “Price Range” still shows counts for all brands, not just Brand X.
  3. Clear the brand filter; facet counts revert to correct totals.

Detection

Fix

Prevention checklist

12. How Persona‑Driven Autonomous Exploration Surfaces These Bugs

Traditional scripted tests follow predetermined paths and data sets, which means they rarely hit the edge‑case combinations that trigger the bugs above. Autonomous QA platforms, such as SUSA, explore an application by simulating real‑world user behaviors—curious, impatient, novice, adversarial, elderly, accessibility‑focused, power‑user, and more. Each persona carries a distinct behavior profile:

During an autonomous run, the platform builds a state graph of screens, actions, and outcomes. When it encounters a mismatch between expected and observed UI (e.g., item count mismatch, unexpected order, accessibility violation), it logs a defect with reproduction steps, screenshots, and console output. Because the exploration is guided by heuristics rather than static scripts, it discovers bugs that only appear under specific interleavings of actions—exactly the scenarios described in sections 2‑11.

How to integrate SUSA into your workflow

  1. Upload the APK or point the CLI at your staging URL: susatest-agent test https://staging.example.com.
  2. Select the persona suite you care about (e.g., --persona curious --persona impatient --persona accessibility ).
  3. Enable cross‑session learning so subsequent runs avoid re‑exploring dead ends and focus on new edge cases.
  4. Review the generated report: each filter‑or‑sorting bug appears with a PASS/FAIL verdict, a short video, and the exact sequence of taps, scrolls, and inputs that triggered it.
  5. Export regression scripts: SUSA can output Appium (Android) or Playwright (Web) test cases that you can add to your CI pipeline for continuous verification.

By combining persona‑driven exploration with the manual and automated techniques outlined below, you gain coverage that scripted tests alone cannot provide.

13. Building a Test Matrix for Filters and Sorting

A systematic matrix helps you verify that each combination of filter type, sort direction, edge‑case data, and user persona is exercised. Below is a compact example you can extend to your own domain.

DimensionValues
Filter typeText equality, Text contains, Numeric range, Date range, Multi‑select checkbox, Toggle switch
Sort fieldTimestamp, Price, Name (alphanumeric), Rating, Custom score
Sort directionAscending, Descending
Data edge casesEmpty set, Single item, Exact page size multiple, One‑item remainder, Null/NaN values, Unicode characters, Duplicate primary keys
User personaCurious, Impatient, Novice, Adversarial, Accessibility, Elderly, Power user
Action sequenceApply filter → Sort → Change page → Clear filter → Re‑apply → Rapid toggle

How to use the matrix

  1. Pick a row (e.g., Filter type = Numeric range, Sort field = Price, Sort direction = Descending, Data edge case = Null/NaN values, Persona = Adversarial).
  2. Execute the prescribed action sequence on a test environment populated with the edge‑case dataset.
  3. Record pass/fail criteria: correct item order, no missing or duplicated items, accessible controls, stable facet counts, response time < 200 ms.
  4. Automate the row as a parameterized test (see code snippet in the next section) and add it to your nightly suite.

This matrix guarantees that you exercise not just the “happy path” but also the boundary conditions where filters and sorting bugs tend to hide.

14. Manual and Automated Detection Techniques

14.1 Manual exploratory testing checklist

StepWhat to verify
Load page with default stateNo errors, initial list matches backend count
Apply each filter individuallyList updates, facet counts reflect new subset
Combine two or more filtersResult matches logical AND/OR as specified
Clear all filtersList returns to original unfiltered state
Sort by each column (asc/desc)Order matches comparator, ties resolved deterministically
Rapidly toggle filters/sortsNo stale UI flashes, no request race conditions
Keyboard navigation (Tab, Enter, Esc)All controls reachable, focus managed correctly
Screen‑reader narrationState changes announced (e.g., “5 items shown after filter”)
Performance check (large dataset)Sort/action completes within 200 ms, no main‑thread block > 50 ms
Network throttling (Slow 3G)No out‑of‑order responses visible to user

Run this checklist for each major filter/sort UI in your application. Document any deviation as a bug ticket with steps, expected vs. actual, and environment details.

14.2 Automated unit / contract tests

Example: testing a safe numeric comparator (JavaScript)


// safeNumberComparator.js
export function safeNumberComparator(a, b) {
  if (a === null && b === null) return 0;
  if (a === null) return 1;
  if (b === null) return -1;
  if (Number.isNaN(a) && Number.isNaN(b)) return 0;
  if (Number.isNaN(a)) return 1;
  if (Number.isNaN(b)) return -1;
  return a - b;
}

// safeNumberComparator.test.js
import { safeNumberComparator } from './safeNumberComparator.js';

describe('safeNumberComparator', () => {
  test('handles nulls', () => {
    expect(safeNumberComparator(null, 5)).toBe(1);
    expect(safeNumberComparator(5, null)).toBe(-1);
    expect(safeNumberComparator(null, null)).toBe(0);
  });

  test('handles NaN', () => {
    expect(safeNumberComparator(NaN, 2)).toBe(1);
    expect(safeNumberComparator(2, NaN)).toBe(-1);
    expect(safeNumberComparator(NaN, NaN)).toBe(0);
  });

  test('orders numbers correctly', () => {
    const arr = [9, -3, 0, 7, NaN, null];
    const sorted = [...arr].sort(safeNumberComparator);
    expect(sorted).toEqual([null, NaN, -3, 0, 7, 9]);
  });
});

Example: contract test for pagination offset (using Pact)


// pact/consumer/pagination.test.js
const { Pact } = require('@pact-foundation/pact');
const fetch = require('node-fetch');

describe('

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 11 real users — finds bugs, accessibility violations, and security issues. No scripts. New to the category? Start with what autonomous product intelligence & QA means.

Try SUSA Free