Dan D
Dan D

Reputation: 17

Sort array with uasort

I’m working on a little PHP function that will sort an array based off of if the item returned isVerified or not. If it is in fact verified, I'd like those results to be displayed at the top of the list. I admit I’m not the most familiar with PHP and I’ve been doing a lot of reading up since I figured there could possibly be a method to make this easier. Reading around it sounded like the uasort() was what I needed for its sorting abilities and that it maintains index association, but I’m a little confused as how to properly use it. The original block of PHP I am working on looks like this:

<?php
  if (count($results)):

  $lexisIndex = 1;

  foreach ($results as $person){
    $person->isVerified = (substr($person->first_name, 0, 1) == substr($fname, 0, 1) && $person->last_name == $lname );                                     
    $person->lexisIndex = $lexisIndex++;
  }

  foreach ($results as $person):
?>

From here I put the $results into an array and passed it into a uasort function. After I added what I thought I needed the code looked like this:

<?php
    if (count($results)):

        $lexisIndex = 1;

        foreach ($results as $person){
            $person->isVerified = (substr($person->first_name, 0, 1) ==  substr($fname, 0, 1) && $person->last_name == $lname );
            $person->lexisIndex = $lexisIndex++;
        }

        $array = array($results);

        uasort($array, function($a, $b) {
        if ($a == $b) {
            return 0;
        }
        return ($a < $b ? -1 : 1);
    });

    foreach ($results as $person):
?>

This still didn't work for me and didn't throw any errors so I'm having a heck of a time trying to work this one out. Am I on the right track with what I'm using? Or can this be done in a more efficient manner? I apologize if this is sounds like a question that has been asked before, but I couldn't find a fix while searching around. If this is a duplicate I'm more than happy to click a link and do some more reading. Thanks for your time and help.

Upvotes: 0

Views: 248

Answers (1)

GolezTrol
GolezTrol

Reputation: 116140

$a and $b are the elements of the array. So in your case, they are person objects, and that's how you should compare them:

uasort($array, function($a, $b) {
    // isVerified is most important. Move to top.
    if ($a->isVerified != $b->isVerified ) {
      return ($a->verified ? -1 : 1);
    }
    // If neither or both are verified, compare based on lexisindex.
    return $a->lexisIndex - $b->lexisIndex;
});

The last line is just a shortcut. A comparison function doesn't need to return -1, 0 or 1. It just accepts any value < 0 to indicate that $a is 'smaller' than $b, 0 for equality or > 0 for larger. So your current compare callback, which would compare numeric values, could also just be written as return $a - $b;. You could also write the last line like this, but it's not necessary:

return ($a->lexisIndex < $b->lexisIndex ? -1 : 1);

Upvotes: 1

Related Questions