From c5d39db63bc6f12d0bcbdebe1d4dacf13d5d6edd Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Mon, 6 Oct 2014 18:27:53 -0400 Subject: [PATCH] Brought Sort back into the repo Decided that having a bunch of external dependencies would end up being more trouble than it's worth --- src/Sort.php | 70 ++++++++++++++++++++++++++++++++++++++ tests/Pickles/SortTest.php | 61 +++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 src/Sort.php create mode 100644 tests/Pickles/SortTest.php diff --git a/src/Sort.php b/src/Sort.php new file mode 100644 index 0000000..5c2a4d9 --- /dev/null +++ b/src/Sort.php @@ -0,0 +1,70 @@ +' : '<') .' $b) ? -1 : 1; + ')); + + return true; + } +} + diff --git a/tests/Pickles/SortTest.php b/tests/Pickles/SortTest.php new file mode 100644 index 0000000..ed916f2 --- /dev/null +++ b/tests/Pickles/SortTest.php @@ -0,0 +1,61 @@ + 'epsilon'], + ['name' => 'gamma'], + ['name' => 'alpha'], + ['name' => 'delta'], + ['name' => 'beta'], + ]; + + $sorted = [ + ['name' => 'alpha'], + ['name' => 'beta'], + ['name' => 'delta'], + ['name' => 'epsilon'], + ['name' => 'gamma'], + ]; + + Pickles\Sort::by('name', $shuffled); + + $this->assertEquals($sorted, $shuffled); + } + + public function testByNameDESC() + { + $shuffled = [ + ['name' => 'epsilon'], + ['name' => 'gamma'], + ['name' => 'alpha'], + ['name' => 'delta'], + ['name' => 'beta'], + ]; + + $sorted = [ + ['name' => 'gamma'], + ['name' => 'epsilon'], + ['name' => 'delta'], + ['name' => 'beta'], + ['name' => 'alpha'], + ]; + + Pickles\Sort::by('name', $shuffled, Pickles\Sort::DESC); + + $this->assertEquals($sorted, $shuffled); + } + + public function testMissingField() + { + $shuffled = [['foo' => 'bar', 'bar' => 'foo']]; + $sorted = [['foo' => 'bar', 'bar' => 'foo']]; + + Pickles\Sort::by('name', $shuffled); + + $this->assertEquals($sorted, $shuffled); + } +} +