Download the PHP package rikudou/array-sort without Composer
On this page you can find all versions of the php package rikudou/array-sort. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rikudou/array-sort
More information about rikudou/array-sort
Files in rikudou/array-sort
Package array-sort
Short Description Class for sorting arrays
License WTFPL
Homepage https://github.com/RikudouSage/php-array-sort
Informations about the package array-sort
PHP ArraySort
Simple class for sorting arrays, it's just a wrapper around built-in php functions.
All sorting methods are performed on a copy of array, they don't modify the original array
and return arrays. Or throw \rikudou\ArraySortException
.
Installation
Use composer: composer require rikudou/array-sort
Usage
For a better control the sorting methods are chained by the type.
Example:
All methods are chained this way.
Types:
byValue()
maintainKeys()
discardKeys()
byKey()
Magic methods
This class can also be used with magic methods, so you don't have to write the whole chain.
Example:
The behavior is controlled by the precedence of classes, which by default is:
- Sort by value
- Maintain key
- Discard key
- Sort by key
The order can be changed by using static method setOrder()
with the help of
ArraySortPrecedenceConstants
class.
Example:
This sets that magic methods will by default be looked up in the by key sorting methods.
You don't have to specify all of the order, the rest will be appended automatically in default order.
So the above translates to this:
- Sort by key
- Sort by value
- Maintain key
- Discard key
Another example:
This means:
- Sort by value
- Discard key
- Maintain key
- Sort by key
Of course you can also set the whole order:
This translates to:
- Sort by key
- Sort by value
- Discard key
- Maintain key
The magic methods use reflection so it's kind of slow compared to normal chaining, but it also uses a cache, so if it finds a method in a class once, further on it uses the class without looking it up.
If you change the order, the cache is flushed and all magic method calls have to be looked up again.
All magic methods have a docblock comment so IDE should be able to hint the method names.