Changeset 7426
- Timestamp:
- 10/14/2013 11:28:45 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-core/bp-core-functions.php
r7334 r7426 83 83 84 84 return apply_filters( 'bp_core_get_table_prefix', $wpdb->base_prefix ); 85 } 86 87 /** 88 * Sort an array of objects or arrays by alphabetically sorting by a specific key/property. 89 * 90 * For instance, if you have an array of WordPress post objects, you can sort 91 * them by post_name as follows: 92 * $sorted_posts = bp_alpha_sort_by_key( $posts, 'post_name' ); 93 * 94 * The main purpose for this function is so that you can avoid having to create 95 * your own awkward callback function for usort(). 96 * 97 * @param array $items The array to be sorted. Its constituent items can be 98 * either associative arrays or objects. 99 * @param string|int $key The array index or property name to sort by. 100 * @return array $items The sorted array. 101 */ 102 function bp_alpha_sort_by_key( $items, $key ) { 103 usort( $items, create_function( '$a, $b', ' 104 $values = array( 0 => false, 1 => false, ); 105 foreach ( func_get_args() as $indexi => $index ) { 106 if ( isset( $index->' . $key . ' ) ) { 107 $values[ $indexi ] = $index->' . $key . '; 108 } else if ( isset( $index["' . $key . '"] ) ) { 109 $values[ $indexi ] = $index["' . $key . '"]; 110 } 111 } 112 113 if ( $values[0] && $values[1] ) { 114 $cmp = strcmp( $values[0], $values[1] ); 115 if ( 0 > $cmp ) { 116 $retval = -1; 117 } else if ( 0 < $cmp ) { 118 $retval = 1; 119 } else { 120 $retval = 0; 121 } 122 return $retval; 123 } else { 124 return 0; 125 } 126 ') ); 127 128 return $items; 85 129 } 86 130 -
trunk/tests/testcases/core/functions.php
r7104 r7426 188 188 } 189 189 190 /** 191 * @group bp_alpha_sort_by_key 192 */ 193 public function test_bp_alpha_sort_by_key_arrays() { 194 $items = array( 195 array( 196 'foo' => 'bar', 197 'name' => 'alpha', 198 ), 199 array( 200 'foo' => 'bar', 201 'name' => 'charlie', 202 ), 203 array( 204 'foo' => 'bar', 205 'name' => 'beta', 206 ), 207 ); 208 209 $expected = array( 210 array( 211 'foo' => 'bar', 212 'name' => 'alpha', 213 ), 214 array( 215 'foo' => 'bar', 216 'name' => 'beta', 217 ), 218 array( 219 'foo' => 'bar', 220 'name' => 'charlie', 221 ), 222 ); 223 224 $this->assertEquals( $expected, bp_alpha_sort_by_key( $items, 'name' ) ); 225 } 226 227 /** 228 * @group bp_alpha_sort_by_key 229 */ 230 public function test_bp_alpha_sort_by_key_objects() { 231 $items = array( 232 new stdClass, 233 new stdClass, 234 new stdClass, 235 ); 236 $items[0]->foo = 'bar'; 237 $items[0]->name = 'alpha'; 238 $items[1]->foo = 'bar'; 239 $items[1]->name = 'charlie'; 240 $items[2]->foo = 'bar'; 241 $items[2]->name = 'beta'; 242 243 $expected = array( 244 new stdClass, 245 new stdClass, 246 new stdClass, 247 ); 248 $expected[0]->foo = 'bar'; 249 $expected[0]->name = 'alpha'; 250 $expected[1]->foo = 'bar'; 251 $expected[1]->name = 'beta'; 252 $expected[2]->foo = 'bar'; 253 $expected[2]->name = 'charlie'; 254 255 $this->assertEquals( $expected, bp_alpha_sort_by_key( $items, 'name' ) ); 256 } 257 190 258 }
Note: See TracChangeset
for help on using the changeset viewer.