Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 2 min read

The One PHP Sorting Mistake That Silently Breaks Your Data

I have watched the same bug ship to production more than once, and it never looks like a bug. A list of products comes back from the database, the page sorts them by price, and everything looks right β€” until a customer n

I have watched the same bug ship to production more than once, and it never looks like a bug. A list of products comes back from the database, the page sorts them by price, and everything looks right β€” until a customer notices the cheapest item is not first, or a row that should sit at the top quietly sinks to the bottom. No error. No warning. Just wrong order.

The cause is almost always one line: sort() called on a multidimensional array.

Why sort() fails on rows

sort() knows how to order simple values β€” numbers, strings, a flat list. Hand it an array of arrays and it compares whole rows using PHP's standard comparison, which looks at the first field where two rows differ. If your rows start with an id field, congratulations: you just sorted by id, not by price. The code runs, the page renders, and the data is wrong in a way nobody notices until it matters.

// Looks right. Sorts by id, not by price.
sort($products);

The fix: tell PHP which field decides

usort() takes a comparison function, and the spaceship operator <=> returns exactly what it needs β€” negative when the first row comes first, positive when the second does, zero when they tie:

usort($products, fn($a, $b) => $a['price'] <=> $b['price']);

One line, and the sort finally means what you meant. Swap $a and $b for descending order. Need a tiebreaker β€” say, more stock first when two prices match? Check the keys in order and return as soon as one decides:

usort($products, function ($a, $b) {
    if ($a['price'] !== $b['price']) {
        return $a['price'] <=> $b['price'];
    }
    return $b['stock'] <=> $a['stock'];
});

The second trap: your keys are gone

usort() renumbers the array from zero. If your rows are keyed by user id or product id, that mapping is destroyed the moment the sort runs β€” and any code that later reads $products[3] expecting a specific row is now reading the wrong one. When keys carry meaning, use uasort() instead. It sorts by value and leaves every key attached to its row:

uasort($byId, fn($a, $b) => $a['price'] <=> $b['price']);

The third trap: dates that lie

Dates in YYYY-MM-DD format sort fine as plain strings. But the moment a date arrives as 25/11/2026, string comparison puts the day first β€” and strtotime() will not rescue you, because for slash-separated dates it assumes the American month-first order and returns false. The correct tool is DateTime::createFromFormat(), where you state the format yourself.

I wrote up all five methods β€” usort(), array_multisort(), multi-key sorting, uasort(), and the date trap β€” with runnable examples in the full tutorial: https://rajadevsolutions.online/blog/how-to-sort-a-multidimensional-array-in-php

The rule I now follow is simple: never call sort() on rows, always name the field that decides the order, and always test with two rows that share the same value. That last check is the one that catches the bugs the first two rules miss.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.