phparrayssortingmultidimensional-arraycustom-sort

Sort a 2d array to have a sticky row first, then sort the rest by another column


I have an array that looks similar to this:

Array
(
  [0] => stdClass Object
    (
      [Leasing] => 12939.74
      [Name] => Jeremy
      [Rental] => 0
      [Sales] => 56603.13
      [Total] => 69542.87
    )
  [1] => stdClass Object
    (
      [Leasing] => 0
      [Name] => Shaun
      [Rental] => 0
      [Sales] => 58590
      [Total] => 58590
    )
  [2] => stdClass Object
    (
      [Leasing] => 0
      [Name] => Lindsay
      [Rental] => 0
      [Sales] => 22951.97
      [Total] => 22951.97
    )
  [3] => stdClass Object
    (
      [Leasing] => 0
      [Name] => Sally
      [Rental] => 1200
      [Sales] => 21624.9
      [Total] => 22824.9
    )
  [4] => stdClass Object
    (
      [Leasing] => 0
      [Name] => House
      [Rental] => 0
      [Sales] => 16235.81
      [Total] => 16235.81
    )
  [5] => stdClass Object
    (
      [Leasing] => 5298.85
      [Name] => Bill
      [Rental] => 1200
      [Sales] => 0
      [Total] => 6498.85
    )
)

Currently, the array is sorted by total using this:

usort($data, function ($a, $b) {
    return $b->Total - $a->Total;
});

Now, I need to be able to ALWAYS have the person with [Name] => House to the top of the array. My thoughts are that I can leave it sorted by Total (because I still need it that way) and then take the element with the House value and put it at the start of the array. I can take a particular KEY and put it at the top, but the KEY's may change depending on who has the highest total. How can I always put the person named House at the top of the array?


Solution

  • This should work:

    usort($data, function ($a, $b) {
        if ($a->Name != "House" && $b->Name == "House") {
            return 1;
        } elseif ($a->Name == "House" && $b->Name != "House") {
            return -1;
        } else {
            return $b->Total - $a->Total;
        }
    });
    

    From PHP: usort - Manual:

    The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

    In this case, return 1 tells the sorting function that House is greater than any other value, and -1 that House is lesser than any other value.