Patrioticcow
Patrioticcow

Reputation: 27038

how to sort an object in php?

i have an property $this->result that holds an object:

array(40) {
  [0] => object(Model)#181 (7) {
    ["_id":protected] => string(2) "1"
    ["_img":protected] => string(95) "/1273720855.jpg"
  }
  [1] => object(Model)#224 (7) {
    ["_id":protected] => string(2) "2"
    ["_img":protected] => string(95) "/test.jpg"
  }
  [2] => object(Model)#182 (7) {
    ["_id":protected] => string(2) "3"
    ["_img":protected] => string(95) "/127377775.jpg"
  }
  [3] => object(Model)#224 (7) {
    ["_id":protected] => string(2) "4"
    ["_img":protected] => string(95) "/test.jpg"
  }
  [4] => object(Model)#182 (7) {
    ["_id":protected] => string(2) "5"
    ["_img":protected] => string(95) "/129586775.jpg"
  }
...

so if i do a loop i could get the img property:

foreach($this->result as $val){
    echo $val->getImg(); //'/1273720855.jpg'
}

What i would like to do is to sort the object so that the property's that has /test.jpg to be last or echo them last, like:

array(40) {
  [2] => object(Model)#182 (7) {
    ["_id":protected] => string(2) "3"
    ["_img":protected] => string(95) "/127377775.jpg"
  }
  [4] => object(Model)#182 (7) {
    ["_id":protected] => string(2) "5"
    ["_img":protected] => string(95) "/129586775.jpg"
  }
  [1] => object(Model)#224 (7) {
    ["_id":protected] => string(2) "2"
    ["_img":protected] => string(95) "/test.jpg"
  } 
  [3] => object(Model)#224 (7) {
    ["_id":protected] => string(2) "4"
    ["_img":protected] => string(95) "/test.jpg"
  }
....

i am interested in any solutions, even if i have to create a new array that i can sort afterwords, etc

ant ideas? thanks

Upvotes: 2

Views: 1533

Answers (2)

Baba
Baba

Reputation: 95101

I can see you have over 40 Images and it can still grow etc. .. and am not sure where you are getting it from but can just use Heap to store your images and it would be sorted automatically ...

$heap = new ImageStorage();


// Porpulate Storage form source 
$images = array(1 => "/1273720855.jpg",2 => "/test.jpg",3 => "/127377775.jpg",4 => "/test.jpg",5 => "/129586775.jpg");
foreach ( $images as $id => $img ) {
    $heap->insert(new Model($id, $img));
}

// Simple Output
echo "<pre>";
foreach ( $heap as $img ) {
    echo $img->getImg(), PHP_EOL;
}

Output

/1273720855.jpg
/127377775.jpg
/129586775.jpg
/test.jpg
/test.jpg

Class Used

// Image Sotrage Class
class ImageStorage extends SplHeap {
    public function compare($a, $b) {
        return strcmp($b->getImg(), $a->getImg());
    }
}

// Emulation of your Model class
class Model {
    protected $_id;
    protected $_img;
    function __construct($id, $img) {
        $this->_id = $id;
        $this->_img = $img;
    }

    public function getID() {
        return $this->_id;
    }

    public function getImg() {
        return $this->_img;
    }
}

Upvotes: 2

Michael Blake
Michael Blake

Reputation: 125

What you're after is usort.

http://php.net/usort

bool usort ( array &$array , callable $cmp_function )

You write the function to run the sort, and pass this function as parameter 2. PHP will then loop over the array and run that function against each value in the array.

So you should have something resembling this:

<?php
function cmp($a, $b)
{
    return strcmp($a->getImg(), $b->getImg());
}

usort($this->result, "cmp");

Upvotes: 4

Related Questions