Changeset 9191
- Timestamp:
- 11/26/2014 06:21:42 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-functions.php
r9180 r9191 94 94 95 95 /** 96 * Sort an array of objects or arrays by alphabetically sorting by a specific key/property. 97 * 98 * For instance, if you have an array of WordPress post objects, you can sort 99 * them by post_name as follows: 100 * $sorted_posts = bp_alpha_sort_by_key( $posts, 'post_name' ); 96 * Sort an array of objects or arrays by a specific key/property. 101 97 * 102 98 * The main purpose for this function is so that you can avoid having to create 103 99 * your own awkward callback function for usort(). 104 100 * 105 * @since BuddyPress ( 1.9.0)106 * 107 * @param array $items The array to be sorted. Its constituent items can be108 * either associative arrays or objects.109 * @param string|int $key The array index or property name to sort by.110 * @return array $items The sorted array.111 */ 112 function bp_ alpha_sort_by_key( $items, $key) {101 * @since BuddyPress (2.2.0) 102 * 103 * @param array $items The items to be sorted. Its constituent items can be either associative arrays or objects. 104 * @param string|int $key The array index or property name to sort by. 105 * @param string $type Sort type. 'alpha' for alphabetical, 'num' for numeric. Default: 'alpha'. 106 * @return array $items The sorted array. 107 */ 108 function bp_sort_by_key( $items, $key, $type = 'alpha' ) { 113 109 usort( $items, create_function( '$a, $b', ' 114 110 $values = array( 0 => false, 1 => false, ); … … 123 119 124 120 if ( $values[0] && $values[1] ) { 125 $cmp = strcmp( $values[0], $values[1] ); 121 if ( "num" === "' . $type . '" ) { 122 $cmp = $values[0] - $values[1]; 123 } else { 124 $cmp = strcmp( $values[0], $values[1] ); 125 } 126 126 127 if ( 0 > $cmp ) { 127 128 $retval = -1; … … 138 139 139 140 return $items; 141 } 142 143 /** 144 * Sort an array of objects or arrays by alphabetically sorting by a specific key/property. 145 * 146 * For instance, if you have an array of WordPress post objects, you can sort 147 * them by post_name as follows: 148 * $sorted_posts = bp_alpha_sort_by_key( $posts, 'post_name' ); 149 * 150 * @since BuddyPress (1.9.0) 151 * 152 * @param array $items The items to be sorted. Its constituent items can be either associative arrays or objects. 153 * @param string|int $key The array index or property name to sort by. 154 * @return array $items The sorted array. 155 */ 156 function bp_alpha_sort_by_key( $items, $key ) { 157 return bp_sort_by_key( $items, $key, 'alpha' ); 140 158 } 141 159 -
trunk/tests/phpunit/testcases/core/functions.php
r9180 r9191 188 188 } 189 189 190 /** 191 * @group bp_sort_by_key 192 */ 193 public function test_bp_sort_by_key_arrays_num() { 194 $items = array( 195 array( 196 'foo' => 'bar', 197 'value' => 5, 198 ), 199 array( 200 'foo' => 'bar', 201 'value' => 10, 202 ), 203 array( 204 'foo' => 'bar', 205 'value' => 1, 206 ), 207 ); 208 209 $expected = array( 210 array( 211 'foo' => 'bar', 212 'value' => 1, 213 ), 214 array( 215 'foo' => 'bar', 216 'value' => 5, 217 ), 218 array( 219 'foo' => 'bar', 220 'value' => 10, 221 ), 222 ); 223 224 $this->assertEquals( $expected, bp_sort_by_key( $items, 'value', 'num' ) ); 225 } 226 227 /** 228 * @group bp_sort_by_key 229 */ 230 public function test_bp_sort_by_key_objects_num() { 231 $items = array( 232 new stdClass, 233 new stdClass, 234 new stdClass, 235 ); 236 $items[0]->foo = 'bar'; 237 $items[0]->value = 5; 238 $items[1]->foo = 'bar'; 239 $items[1]->value = 10; 240 $items[2]->foo = 'bar'; 241 $items[2]->value = 1; 242 243 $expected = array( 244 new stdClass, 245 new stdClass, 246 new stdClass, 247 ); 248 $expected[0]->foo = 'bar'; 249 $expected[0]->value = 1; 250 $expected[1]->foo = 'bar'; 251 $expected[1]->value = 5; 252 $expected[2]->foo = 'bar'; 253 $expected[2]->value = 10; 254 255 $this->assertEquals( $expected, bp_sort_by_key( $items, 'value', 'num' ) ); 256 } 190 257 /** 191 258 * @group bp_alpha_sort_by_key
Note: See TracChangeset
for help on using the changeset viewer.