Created
September 7, 2017 01:10
-
-
Save liuxd/cf3731c4fe156c9fd2a90e5fd46cb3c0 to your computer and use it in GitHub Desktop.
twoFieldSorter.php
This file contains hidden or 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 | |
| /** | |
| * 二维数组(例如:从数据读取一个数据列表。)按两个字段进行排序。使用冒泡排序。 | |
| * | |
| * @param array $arr 待排序数组。 | |
| * @param array $ord 排序因子。 | |
| * 例子:[ | |
| * 0 => 'field1:asc', | |
| * 1 => 'field2:desc', | |
| * ]; | |
| * | |
| * 表示按field1升序排序之后,在field1的值相等的情况下,再按field2降序排序。 | |
| * asc,desc是固定的值,没有其他可能值。 | |
| * @return string | |
| */ | |
| function twoFieldSorter($arr, $ord) | |
| { | |
| if (empty($ord) or empty($arr)) { | |
| return FALSE; | |
| } | |
| $len = count($arr); | |
| $x = $arr; | |
| list ($field1, $ord1) = explode(':', $ord[0]); | |
| list ($field2, $ord2) = explode(':', $ord[1]); | |
| // 第一个字段用usort()函数排序。 | |
| if ($ord1 === 'asc') { | |
| usort($x, function($a, $b)use($field1) { | |
| $r = $a[$field1] - $b[$field1]; | |
| return $r < 0 ? -1 : 1; | |
| }); | |
| } else { | |
| usort($x, function($a, $b)use($field1) { | |
| $r = $a[$field1] - $b[$field1]; | |
| return $r > 0 ? -1 : 1; | |
| }); | |
| } | |
| //二维排序 | |
| for ($j = 0; $j < $len; $j++) { | |
| for ($z = $j + 1; $z < $len; $z++) { | |
| if ($x[$j][$field1] == $x[$z][$field1]) { | |
| if ($ord2 === 'desc') { | |
| if ($x[$j][$field2] < $x[$z][$field2]) { | |
| $tmp = $x[$j]; | |
| $x[$j] = $x[$z]; | |
| $x[$z] = $tmp; | |
| } | |
| } else { | |
| if ($x[$j][$field2] > $x[$z][$field2]) { | |
| $tmp = $x[$j]; | |
| $x[$j] = $x[$z]; | |
| $x[$z] = $tmp; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return $x; | |
| } | |
| # end of this file. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment