Last active
November 25, 2016 05:28
-
-
Save brothertao/b69c60bfd951b1819bd8 to your computer and use it in GitHub Desktop.
sort by field use class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SortHelper { | |
private static $field; | |
static function byField(&$data, $field) { | |
self::$field = $field; | |
return usort($data, array('self', 'cmp')); | |
} | |
private static function cmp($a, $b) { | |
return $a[self::$field] > $b[self::$field]; | |
} | |
} | |
//example | |
$data = array(array('t'=>1, 'p'=>'aBc'), array('t'=>3, 'p'=>'aBb')); | |
$rzt = SortHelper::byField($data, 't'); | |
var_dump($data, $rzt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
对比使用闭包的实现方式:https://gist.github.com/brothertao/d9de53cb43752f38a6cf