Jump to content

Custom Sort an array by object key


beanymanuk

Recommended Posts

Hi

 

How can I custom sort this array into any order I specify by say the object key - name like this

P2,P1,P3

 

or by id

sku991,sku838,sku123,

 

Here is a PHP var dump
 

array(3) {
  [0]=>
  object(stdClass)#212 (2) {
    ["id"]=>
    string(36) "sku838"
    ["name"]=>
    string(61) "P1"
  }
  [1]=>
  object(stdClass)#235 (2) {
    ["id"]=>
    string(36) "sku991"
    ["name"]=>
    string(61) "P2"
  }
  [2]=>
  object(stdClass)#240(2) {
    ["id"]=>
    string(36) "sku123"
    ["name"]=>
    string(61) "P3"
  }
}

Thankyou

Link to comment
Share on other sites

Trying this but it's not working? Not sure where I am going wrong

$order = array(
    'P2',
    'P1',
    'P3'
);

function custom_compare($a, $b){
    global $order;
    //echo $a->id."</br>";
    $a = array_search($a->id, $order);
    //echo $a."</br>";
    $b = array_search($b->id, $order);
    if($a === false && $b === false) { // both items are dont cares
        //echo "HERE1";
        return 0;                      // a == b
    }
    else if ($a === false) {           // $a is a dont care item
        //echo "HERE2";
        return 1;                      // $a > $b
    }
    else if ($b === false) {           // $b is a dont care item
        //echo "HERE3";
        return -1;                     // $a < $b
    }
    else {
        //echo "HERE4";
        return $a - $b;
    }
}
//shuffle($products);  // for testing
//var_dump($products); // before
usort($products, "custom_compare");
//var_dump($products); // after
Link to comment
Share on other sites

  • 5 months later...
  function custom_compare($a, $b){
      global $order;
      //echo $a->id."</br>";
      $a = array_search($a->name, $order);
      //echo $a."</br>";
      $b = array_search($b->name, $order);
      if($a === false && $b === false) { // both items are dont cares
          //echo "HERE1";
          return 0;                      // a == b
      }
      else if ($a === false) {           // $a is a dont care item
          //echo "HERE2";
          return 1;                      // $a > $b
      }
      else if ($b === false) {           // $b is a dont care item
          //echo "HERE3";
          return -1;                     // $a < $b
      }
      else {
          //echo "HERE4";
          return strcmp($a,$b);
      }
  }
  //shuffle($products);  // for testing
  //var_dump($products); // before
  usort($categories, "custom_compare");
  //var_dump($products); // after

So I have this, but this isn't working any idea's what is wrong?

Link to comment
Share on other sites

try

 

$data = [   (object)[ 'id' => 'sku838',
                      'name' => 'P1'  ],
            (object)[ 'id' => 'sku991',
                      'name' => 'P3'  ],         
            (object)[ 'id' => 'sku123',
                      'name' => 'P2'  ]
        ];

$sortBy = 'name';      // define the property to sort by

usort($data, function($a, $b) use ($sortBy) {
                  return strcmp($a->$sortBy, $b->$sortBy) ;
                  });
                  
echo '<pre>', print_r($data, 1), '</pre>';        
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.