Download the PHP package tuck/sort without Composer
On this page you can find all versions of the php package tuck/sort. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package sort
Tuck\Sort
Syntactic sugar for PHP's built in sorting.
Examples
Basic sorting functions
Why?
This library tries to smooth out the built-in API, specifically the following issues:
PHP requires variables.
If you want to sort the result of a method, you need to assign it to an intermediate variable.
With this library, you can pass results directly:
PHP modifies in-place
Perhaps you'd prefer sort to return a modified list, rather than mutating the original. Unfortunately, the return value of is a success boolean, not the reordered list.
With this library, the results are returned directly and the original list is not modified:
PHP's naming is confusing
PHP's sorting functions don't always have the most intuitive names:
With this library, naming reads a little better:
PHP doesn't handle keys consistently
When you use PHP discards the original keys. Unless you use . Or and which always preserve the original keys. You get the idea.
With this library, the returned keys are reset to sequential numbers, regardless of the type of sorting.
If you'd like to keep the original keys, there's a consistent parameter you can always use . This selects the correct key-preserving function under the hood, so you don't have to remember it.
This works for all sort functions:
Much easier to remember. If the constants are too long-winded or readable, you can also pass to preserve the keys or to discard them.
PHP doesn't accept iterators or generators
Built-in sorting functions work great on arrays but won't accept their Traversable brethren.
With this library, iterators and generators are automatically converted to arrays.
Note this library always returns arrays, even when given a custom collection. If you'd like your custom collection to support this library, see the usort documentation below.
PHP doesn't have a shorthand for sorting by fields
When you're comparing a list of objects, you usually want to compare the same field on them repeatedly. Usually that means writing a usort function like this:
And that's with the PHP 7 shorthand operator helping. This library offers a slightly shorter, Scala inspired version where you only specify how to retrieve the sortable data from an element.
This also works in reverse:
PHP doesn't have chained usort
There's no elegant syntax for chaining multiple sorts at once. If you wanted to sort a list of high scores, first by points, then name, then date, you'd need to write a function like:
With this library, you can chain sorts like so:
In most cases, you'll want to extract the same information from and at the same time. You might also want to sort them as ascending or descending on different factors. For both of these use cases, you can use the and methods.
Once you've created your sorting chain, you can apply it to keys or values. Features like Iterator support, returned values, and PRESERVE_KEY flag are all supported.
You can also invoke the chain itself as a comparison function:
This means you can use it with any custom collection class that supports usort:
When building your sort chain, you don't have to compose all the sorting functions at once. You can attach them over time or conditionally:
However, if use the chain before any sorts are added, then the order is essentially random and decided by your PHP runtime (PHP 5.x vs PHP 7 vs HHVM). There's no easy way to normalize this (I've tried) but it varies based on the runtime, the number of elements in the array, the sorting algorithm used, etc. The good news is it doesn't matter if the elements are random because there was nothing worth sorting by anyways! :)
License
Contributing
PRs welcome. :) Please abide by PSR-2, use tests, and respect the Code Manifesto.
Status
Working, tested, still need to add non-functionals like docs and fancy README badges.