Skip to content

Commit 55dc12f

Browse files
committed
Addded sort function to the GenericCollection so any collection can be sorted by a given closure.
1 parent 9003297 commit 55dc12f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

lib/Tmdb/Model/Common/GenericCollection.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,30 @@ public function filter(\Closure $closure, $static = true)
289289
return $collection;
290290
}
291291

292+
/**
293+
* Allows sorting the current collection.
294+
*
295+
* For example:
296+
*
297+
* $person->sort(function ($a, $b) {
298+
* if ($a->getReleaseDate() == $b->getReleaseDate()) {
299+
* return 0;
300+
* }
301+
*
302+
* return $a->getReleaseDate() < $b->getReleaseDate() ? 1 : -1;
303+
* });
304+
*
305+
*
306+
* @param callable $closure
307+
* @return $this
308+
*/
309+
public function sort(\Closure $closure)
310+
{
311+
uasort($this->data, $closure);
312+
313+
return $this;
314+
}
315+
292316
public function offsetExists($offset)
293317
{
294318
return isset($this->data[$offset]);

0 commit comments

Comments
 (0)