@ 2017-07-11T15:46:37Z <?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 array sorted by sortBy().
*/
public static function sortBy( array &$array, $sortKey = null, $dir = 'ASC', $nat = false ) {
if ( null === static::$instance ) {
static::$instance = new static();
};
function sortLogic( $key, $dir, bool $nat ) {
return function( $a, $b ) use ( $key, $dir, $nat ) {
if ( true === $nat ){
return strnatcmp( $a : $b )[$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', 'ASC', 'cat' );
print_r( $data );
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.1.7 0.012 0.009 17.15 7.1.6 0.022 0.022 34.96 7.1.5 0.036 0.009 34.77 7.1.4 0.033 0.003 34.33 7.1.3 0.025 0.012 34.35 7.1.2 0.028 0.016 34.50 7.1.1 0.009 0.012 16.30 7.1.0 0.010 0.010 16.42 7.0.20 0.007 0.012 16.69 7.0.19 0.010 0.009 16.71 7.0.18 0.010 0.010 15.97 7.0.17 0.012 0.008 16.00 7.0.16 0.011 0.011 16.12 7.0.15 0.009 0.012 15.97 7.0.14 0.011 0.010 16.07 7.0.13 0.009 0.013 16.07 7.0.12 0.009 0.012 16.38 7.0.11 0.009 0.008 16.16 7.0.10 0.002 0.016 16.07 7.0.9 0.013 0.007 16.15 7.0.8 0.007 0.014 15.91 7.0.7 0.005 0.015 16.01 7.0.6 0.007 0.014 15.70 7.0.5 0.014 0.007 16.36 7.0.4 0.005 0.016 16.49 7.0.3 0.009 0.009 16.45 7.0.2 0.003 0.016 16.33 7.0.1 0.009 0.009 16.33 7.0.0 0.008 0.008 16.19
preferences:dark mode live preview
139.43 ms | 1394 KiB | 7 Q