graze / sort
A collection of array sorting transforms and functions
Installs: 244 706
Dependents: 2
Suggesters: 0
Security: 0
Stars: 12
Watchers: 21
Forks: 1
Open Issues: 0
Requires
- php: >=5.3
Requires (Dev)
- adlawson/timezone: ~1.0
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-10-24 05:09:40 UTC
README
This library provides a few useful functions used for sorting large arrays or
sorting where comparing array values is expensive to perform. The intention is
to provide these functions while remaining interoperable with PHP's built-in
usort
functions where possible.
It can be installed in whichever way you prefer, but we recommend Composer.
{ "require": { "graze/sort": "*" } }
Documentation
<?php $unsorted = [ (object) ['foo' => 1, 'bar' => 3], (object) ['foo' => 3, 'bar' => 2], (object) ['foo' => 2, 'bar' => 1], (object) ['foo' => 2, 'bar' => 2], (object) ['foo' => 3, 'bar' => 3], (object) ['foo' => 1, 'bar' => 1], (object) ['foo' => 2, 'bar' => 3], (object) ['foo' => 3, 'bar' => 1], (object) ['foo' => 1, 'bar' => 2] ]; $foo = function ($v) { return $v->foo; }; $bar = function ($v) { return $v->bar; }; // Using comparison sorting $sorted = \Graze\Sort\comparison($unsorted, [$foo, $bar]); // Using schwartzian sorting $sorted = \Graze\Sort\schwartzian($unsorted, [$foo, $bar]); // Using comparison sorting with usort $sorted = $unsorted; usort($sorted, \Graze\Sort\comparison_fn($unsorted, [$foo, $bar]));