Skip to content

Commit ef87b2c

Browse files
author
Larry Lewis
committed
Added support for PHP native sort
1 parent fcf46a2 commit ef87b2c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,22 @@ or to check if a constraint is a subset of another constraint.
1818
All that is done to let us select which version is compatible with a user constraints.
1919

2020
It works with the same rules of Composer versioning.
21+
22+
### Sorting
23+
In order to use standard PHP sorting there is a helper class 'Compare' to use it with a sort you will need to do this;
24+
25+
```php
26+
use Version\Version;
27+
use Version\Compare;
28+
29+
$vers = array();
30+
$vers[] = Version::parse('1.1.1d1');
31+
$vers[] = '1.1.1';
32+
33+
$obj = new Compare();
34+
usort( $vers, array( $obj, 'compare' ) );
35+
```
36+
37+
The above should allow any normal PHP sort to be instigated with very little effort.
38+
39+
**Note**: You should see that it will handle a string or a Version object as the variable to sort automatically.

src/Version/Compare.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace Version;
3+
4+
use Version\Version;
5+
6+
class Compare
7+
{
8+
public function compare( $v1, $v2 )
9+
{
10+
$va1 = ( $v1 instanceof Version ) ? $v1 : Version::parse( $v1 );
11+
$va2 = ( $v2 instanceof Version ) ? $v2 : Version::parse( $v2 );
12+
return( $va1->compare( $va2 ) );
13+
}
14+
}

0 commit comments

Comments
 (0)