diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..57872d0
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/vendor/
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..525efe5
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,20 @@
+{
+ "name": "joshtronic/php-sort",
+ "type": "library",
+ "license": "MIT",
+ "require": {
+ "platform": {
+ "php": ">=5.3"
+ }
+ },
+ "autoload": {
+ "psr-4": {
+ "Pickles\\": "src"
+ }
+ },
+ "autoload-dev": {
+ "psr-4": {
+ "Pickles\\Tests\\": "tests"
+ }
+ }
+}
diff --git a/phpcs.xml b/phpcs.xml
new file mode 100644
index 0000000..24d2560
--- /dev/null
+++ b/phpcs.xml
@@ -0,0 +1,5 @@
+
+
+ vendor/*
+
+
diff --git a/phpunit.xml b/phpunit.xml
new file mode 100644
index 0000000..cb568a9
--- /dev/null
+++ b/phpunit.xml
@@ -0,0 +1,4 @@
+
+
+ ./tests
+
diff --git a/src/Sort.php b/src/Sort.php
index 6d37de6..5473208 100644
--- a/src/Sort.php
+++ b/src/Sort.php
@@ -27,7 +27,7 @@ class Sort
*
* Variable to utilize ascending sort
*
- * @var integer
+ * @var string
*/
const ASC = 'ASC';
@@ -36,7 +36,7 @@ class Sort
*
* Variable to utilize descending sort
*
- * @var integer
+ * @var string
*/
const DESC = 'DESC';
@@ -48,23 +48,21 @@ class Sort
* @param string $field field to sort by
* @param array $array array to sort
* @param string $direction optional direction to sort
- * @retun boolean true because sorting is done by reference
+ * @return boolean true because sorting is done by reference
*/
public static function by($field, &$array, $direction = Sort::ASC)
{
- usort($array, create_function('$a, $b', '
- $a = $a["' . $field . '"];
- $b = $b["' . $field . '"];
+ usort($array, function ($a, $b) use ($field, $direction) {
+ $a = $a[$field];
+ $b = $b[$field];
- if ($a == $b)
- {
+ if ($a === $b) {
return 0;
}
- return ($a ' . ($direction == Sort::DESC ? '>' : '<') .' $b) ? -1 : 1;
- '));
+ return ($a < $b ? -1 : 1) * ($direction === Sort::DESC ? -1 : 1);
+ });
return true;
}
}
-
diff --git a/tests/SortTest.php b/tests/SortTest.php
index ed916f2..d29543e 100644
--- a/tests/SortTest.php
+++ b/tests/SortTest.php
@@ -1,6 +1,10 @@
'gamma'],
];
- Pickles\Sort::by('name', $shuffled);
+ Sort::by('name', $shuffled);
$this->assertEquals($sorted, $shuffled);
}
@@ -43,7 +47,7 @@ class SortTest extends PHPUnit_Framework_TestCase
['name' => 'alpha'],
];
- Pickles\Sort::by('name', $shuffled, Pickles\Sort::DESC);
+ Sort::by('name', $shuffled, Sort::DESC);
$this->assertEquals($sorted, $shuffled);
}
@@ -53,9 +57,8 @@ class SortTest extends PHPUnit_Framework_TestCase
$shuffled = [['foo' => 'bar', 'bar' => 'foo']];
$sorted = [['foo' => 'bar', 'bar' => 'foo']];
- Pickles\Sort::by('name', $shuffled);
+ Sort::by('name', $shuffled);
$this->assertEquals($sorted, $shuffled);
}
}
-