Search code examples
phparrayssortingcustom-sort

Sort a flat array using a custom value order


I am still very confused on how to sort a array by certain values. I know you are supposed to use the CMD function and the usort(). Yet I am just confused on what I am suppose to do. I would like to sort my array so the keys "Bronze, "Silver", "Gold", "Platinum" are shown in that order. I am not sure how I would put what I need into the following:

function cmp($a, $b){
   return strcmp(???);
}

So basically I have a array this is listen like this:

"itemname" => "Value"

I would like the whole thing to be ordered by the value of the "Value". Value could be "Bronze"/"Silver"/"gold"/"Platinum". And I would like it to be sorted in that order.

EDIT 2

    $query = "SELECT * FROM postallions WHERE userid = :userid";
$params = array(':userid' => $userid);
try{
    $stmt = $connection->prepare($query);
    $result = $stmt->execute($params);
}
catch(PDOException $ex){
    echo ("Failed to run query: " . $ex->getMessage());
}
$columns = $stmt->fetch();

$postallionlist = $columns;

print_r($postallionlist);

function compareMedals( $a, $b ) {
    $aMap = array(0 => 'None', 1 => 'Bronze', 2 => 'Silver', 3 => 'Gold', 4 => 'Platinum', 5 => 'Uncomplete', 6 => 'Complete');
    $aValues = array( 0, 1, 2, 3, 4, 5, 6);
    $a = str_ireplace($aMap, $aValues, $a);
    $b = str_ireplace($aMap, $aValues, $b);
    return $a - $b;
}
usort($postallionlist, 'compareMedals');
print_r($postallionlist);

EDIT 3 The print_r and then the var_dump:

Array ( [userid] => 1 [0] => 1 [p1] => Silver [1] => Silver [p2] => Platinum [2] => Platinum [p3] => None [3] => None [p4] => None [4] => None [p5] => Bronze [5] => Bronze [p6] => Gold [6] => Gold [p7] => Complete [7] => Complete [p8] => None [8] => None [p9] => None [9] => None [p10] => None [10] => None )
array (size=22)
  'userid' => string '1' (length=1)
  0 => string '1' (length=1)
  'p1' => string 'Silver' (length=6)
  1 => string 'Silver' (length=6)
  'p2' => string 'Platinum' (length=8)
  2 => string 'Platinum' (length=8)
  'p3' => string 'None' (length=4)
  3 => string 'None' (length=4)
  'p4' => string 'None' (length=4)
  4 => string 'None' (length=4)
  'p5' => string 'Bronze' (length=6)
  5 => string 'Bronze' (length=6)
  'p6' => string 'Gold' (length=4)
  6 => string 'Gold' (length=4)
  'p7' => string 'Complete' (length=8)
  7 => string 'Complete' (length=8)
  'p8' => string 'None' (length=4)
  8 => string 'None' (length=4)
  'p9' => string 'None' (length=4)
  9 => string 'None' (length=4)
  'p10' => string 'None' (length=4)
  10 => string 'None' (length=4)

Solution

  • Convert names to positions you like.

    Here's a working prototype:

    $aMedals = array(  
        'adbv'         => 'Gold', 
        'sdgg'         => 'Bronze', 
        'dsgggsd'      =>'Platinum', 
        'dsggsg'       => 'Silver',
        '34g434h34'    => 'Bronze', 
        'g34h4h'       =>'Platinum', 
        'g34h4h'       =>'Gold', 
        'dsg34t43tgsg' => 'Silver'
        );
    var_dump( $aMedals );
    function compareMedals( $a, $b )
    {
        $aMap    = array( 0 => 'Bronze', 1 => 'Silver', 2 => 'Gold', 3 => 'Platinum');
        $aValues = array( 0, 1, 2, 3 );
        $a       = str_ireplace( $aMap, $aValues, $a );
        $b       = str_ireplace( $aMap, $aValues, $b );
        return $a - $b;
    }    
    usort( $aMedals, 'compareMedals');
    var_dump( $aMedals );
    

    Output:

    array(7) {
      [0]=>
      string(6) "Bronze"
      [1]=>
      string(6) "Bronze"
      [2]=>
      string(6) "Silver"
      [3]=>
      string(6) "Silver"
      [4]=>
      string(4) "Gold"
      [5]=>
      string(4) "Gold"
      [6]=>
      string(8) "Platinum"
    }