Skip to content

Instantly share code, notes, and snippets.

@brothertao
Last active November 25, 2016 05:28
Show Gist options
  • Save brothertao/b69c60bfd951b1819bd8 to your computer and use it in GitHub Desktop.
Save brothertao/b69c60bfd951b1819bd8 to your computer and use it in GitHub Desktop.
sort by field use class
<?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);
@brothertao
Copy link
Author

对比使用闭包的实现方式:https://gist.github.com/brothertao/d9de53cb43752f38a6cf

@brothertao
Copy link
Author

brothertao commented Nov 25, 2016

线程不安全,闭包是线程安全的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment