-
Notifications
You must be signed in to change notification settings - Fork 2
/
masort.php
126 lines (115 loc) · 3.66 KB
/
masort.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* Sort an array of associative arrays by one or more keys in the data.
* Do some rudimentary detection of "type" to get correct numeric/lexical
* sort automatically.
*
* Example usage:
*
* $data = array(
* array('flavor' => 'lemon', * 'food' => 'chicken'),
* array('flavor' => 'orange',
* 'food' => 'duck'),
* array('flavor' => 'lemon',
* 'food' => 'sherbert'),
* array('flavor' => 'orange',
* 'food' => 'juice')
* );
* masort($arr, 'flavor_a,food_d'); # "_a" for ascending, "_d" for descending
*
* $data is now:
*
* {
* { 'flavor': 'lemon',
* 'food' : 'sherbert' },
* { 'flavor': 'lemon',
* 'food' : 'chicken' },
* { 'flavor': 'orange',
* 'food' : 'juice' },
* { 'flavor': 'orange',
* 'food' : 'duck' }
* }
*/
// Expects Multidimensional associative array for the first param.
// Second param is "field_a,field_d,field_a" as first param - _a for sort
// ascending, _d for sort descending.
function masort(&$data, $sort) {
if (!$sort || !$data) {
return $data;
}
$function = create_sort_function($sort, $data);
return uasort($data, $function);
}
function create_sort_function($sort, $data) {
$f = '';
foreach (explode(",", $sort) as $raw) {
$ending = substr($raw, -strlen("_d"), strlen("_d"));
if ($ending !== '_a' && $ending !== '_d') {
$ending = '';
}
$key = substr($raw, 0, strlen($raw) - strlen($ending));
$desc = ($ending === "_d");
$cmp = get_comparison_function($key, $data);
$f .= '$res = ' . $cmp . '($a["' . $key . '"], $b["' . $key . '"]); '
. 'if ($res != 0) { '
. 'return ' . ($desc ? '-$res' : '$res') . '; '
. '} ';
}
$f .= 'return $a;';
return create_function('$a, $b', $f);
}
//Look at the data and guess what the best comparator is.
function get_comparison_function($key, $data) {
foreach ($data as $row) {
$value = $row[$key];
if (is_numeric($value)) {
return 'numcmp';
}
}
return 'strcasecmp';
}
function numcmp($a, $b) {
if ($a > $b) {
return 1;
} elseif ($b > $a) {
return -1;
} else {
return 0;
}
}
/* test data
$data = array(
array('A' => 'lemon',
'B' => 'chicken'),
array('A' => 'orange',
'B' => 'duck'),
array('A' => 'lemon',
'B' => 'sherbert'),
array('A' => 'orange',
'B' => 'juice')
);
print "<pre>";
masort("A_a,B_a", $data);
print_r($data);
*/
/*
Copyright (c) 2004 Thomas David Baker
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/