3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php namespace Exercise_One; /** * Zillow exercise one * * Take an associative array and sort it based on columns and directions specified. (PSR1 Standard) * * @author Joe Rocha <joe.rocha@me.com> */ class Singleton { /** * @var Singleton The reference to *Singleton* instance of this class */ protected static $instance; /** * Returns sorted multidimensional, associative array * * @param array $array Data to sort. * @param string $sortKey Key to sort by. * @param string $dir Direction to sort ('ASC' || 'DESC'). * @param bool $nat Natural sorting. * * @return bool $instance True if no other instance. */ public static function sortBy( array &$array, $sortKey = null, $dir = 'ASC', $nat = false ) { if ( null === static::$instance ) { static::$instance = new static(); }; function sortLogic( $key, $dir, $nat ) { return function( $a, $b ) use ( $key, $dir, $nat ) { if ( true === $nat ){ return strnatcmp( 'ASC' === $dir ? $a : $b [ $key ], 'ASC' === $dir ? $b : $a [ $key ] ); } else { return $dir === 'ASC' ? $a[$key] > $b[$key] : $a[$key] < $b[$key]; } }; } uasort( $array, sortLogic( $sortKey, $dir, $nat ) ); return static::$instance; } /** * Make constructor private, so no one can call "new Class". */ private function __construct() {} /** * Make clone magic method private, so no one can clone instance. */ private function __clone() {} /** * Make sleep magic method private, so no one can serialize instance. */ private function __sleep() {} /** * Make wakeup magic method private, so no one can unserialize instance. */ private function __wakeup() {} } // Dummy Data for testing $data = array(); $data[] = array('id' => 1, 'number' => '1', 'street' => 'Battery St', 'unit' => '1', 'rent' => 1200); $data[] = array('id' => 10, 'number' => '10', 'street' => 'Battery St', 'unit' => '3', 'rent' => 1800); $data[] = array('id' => 2, 'number' => '2', 'street' => 'Leavenworth St', 'unit' => '11', 'rent' => 800); $data[] = array('id' => 12, 'number' => '12', 'street' => 'Battery St', 'unit' => '10', 'rent' => 3400); $data[] = array('id' => 1, 'number' => '14', 'street' => 'Leavenworth St', 'unit' => '10', 'rent' => 1450); $data[] = array('id' => 1, 'number' => '01', 'street' => 'Battery St', 'unit' => '5', 'rent' => 1000); // Sanity check Singleton::sortBy( $data, 'number', 'DESC', true ); print_r( $data );
based on aGO3A
Output for hhvm-3.15.4, 7.0.0 - 7.3.0rc3
Array ( [4] => Array ( [id] => 1 [number] => 14 [street] => Leavenworth St [unit] => 10 [rent] => 1450 ) [3] => Array ( [id] => 12 [number] => 12 [street] => Battery St [unit] => 10 [rent] => 3400 ) [1] => Array ( [id] => 10 [number] => 10 [street] => Battery St [unit] => 3 [rent] => 1800 ) [2] => Array ( [id] => 2 [number] => 2 [street] => Leavenworth St [unit] => 11 [rent] => 800 ) [0] => Array ( [id] => 1 [number] => 1 [street] => Battery St [unit] => 1 [rent] => 1200 ) [5] => Array ( [id] => 1 [number] => 01 [street] => Battery St [unit] => 5 [rent] => 1000 ) )
Output for 5.6.0 - 5.6.30
Array ( [4] => Array ( [id] => 1 [number] => 14 [street] => Leavenworth St [unit] => 10 [rent] => 1450 ) [3] => Array ( [id] => 12 [number] => 12 [street] => Battery St [unit] => 10 [rent] => 3400 ) [1] => Array ( [id] => 10 [number] => 10 [street] => Battery St [unit] => 3 [rent] => 1800 ) [2] => Array ( [id] => 2 [number] => 2 [street] => Leavenworth St [unit] => 11 [rent] => 800 ) [5] => Array ( [id] => 1 [number] => 01 [street] => Battery St [unit] => 5 [rent] => 1000 ) [0] => Array ( [id] => 1 [number] => 1 [street] => Battery St [unit] => 1 [rent] => 1200 ) )