php排序usort执行算法,PHP: usort - Manual

Below is a variation on Example #4 that implements a multi-key natural sort on an associative array and can be called in such a way as to allow reversing the sort order and/or making the comparison case insensitive on a key by key basis.

Test code is included in the file – run it from the command line with: php sortUtils.php

*    sortUtils.php

*/

/**

*    mknatsort() - Multi-Key Natural Sort for associative arrays

*

*    Uses the usort() function to perform a natural sort on a multi-dimensional

*    array on multiple keys. Optionally specifying the sort order for each key

*    and/or ignoring the case for each key value.

*

*    @param array $data_array The array to be sorted.

*    @param mixed $keys The list of keys to be sorted by. This may be a single

*        key or an array of keys

*    @param boolean $reverse Specify whether or not to reverse the sort order. If

*        there are multiple keys then $reverse may be an array of booleans - one

*        per key.

*    @param boolean $ignorecase Specify whether or not to ignore the case when

*        comparing key values.reverse the sort order. If there are multiple keys

*        then $ignorecase may be an array of booleans - one per key.

*/functionmknatsort( &$data_array,$keys,$reverse=false,$ignorecase=false) {// make sure $keys is an arrayif (!is_array($keys))$keys= array($keys);usort($data_array,sortcompare($keys,$reverse,$ignorecase) );

}

functionsortcompare($keys,$reverse=false,$ignorecase=false) {

return function ($a,$b) use ($keys,$reverse,$ignorecase) {$cnt=0;// check each key in the order specifiedforeach ($keysas$key) {// check the value for ignorecase and do natural compare accordingly$ignore=is_array($ignorecase) ?$ignorecase[$cnt] :$ignorecase;$result=$ignore?strnatcasecmp($a[$key],$b[$key]) :strnatcmp($a[$key],$b[$key]);// check the value for reverse and reverse the sort order accordingly$revcmp=is_array($reverse) ?$reverse[$cnt] :$reverse;$result=$revcmp? ($result* -1) :$result;// the first key that results in a non-zero comparison determines

// the order of the elementsif ($result!=0) break;$cnt++;

}

return$result;

};

}// end sortcompare()

// test code that will be skipped if this is included in the web environment

// test with: php sortUtils.php from the command line$sapi_type=php_sapi_name();

if (substr($sapi_type,0,3) =='cli') {// the following shows several examples of usage and the resulting output.$test[] = array('Status'=>'In Progress','comment'=>'This looks good.','user_name'=>'John Doe','date_modified'=>'06/13/2014 11:20 AM','role'=>'Senior Account Manager');$test[] = array('Status'=>'In Progress','comment'=>'This looks good, please approve.','user_name'=>'John Doe','date_modified'=>'06/13/2014 11:19 AM','role'=>'Account Manager');$test[] = array('Status'=>'In Progress','comment'=>'This looks good.','user_name'=>'Jane Smith','date_modified'=>'06/13/2014 11:19 AM','role'=>'Senior Account Manager');$test[] = array('Status'=>'Returned','comment'=>'There is not enough informartion.  Please add the following.','user_name'=>'John Doe','date_modified'=>'06/13/2014 11:15 AM','role'=>'Account Manager');$test[] = array('Status'=>'In Progress','comment'=>'I am currently reviewing this.','user_name'=>'John Doe','date_modified'=>'06/13/2014 11:14 AM','role'=>'Account Manager');

echo"Original array: ";print_r($test);

echo"Sorted on date_modified: ";mknatsort($test, array('date_modified') );print_r($test);

echo"Sorted on role(reversed) & date_modified: ";mknatsort($test, array('role','date_modified'),array(true,false) );print_r($test);

echo"Sorted on role & date_modified(reversed): ";mknatsort($test, array('role','date_modified'),array(false,true) );print_r($test);

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值