Changeset 11082
- Timestamp:
- 09/10/2016 05:14:34 PM (8 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-functions.php
r11081 r11082 109 109 * 110 110 * @since 2.2.0 111 * 112 * @param array $items The items to be sorted. Its constituent items can be either associative arrays or objects. 113 * @param string|int $key The array index or property name to sort by. 114 * @param string $type Sort type. 'alpha' for alphabetical, 'num' for numeric. Default: 'alpha'. 111 * @since 2.7.0 Added $preserve_keys parameter. 112 * 113 * @param array $items The items to be sorted. Its constituent items 114 * can be either associative arrays or objects. 115 * @param string|int $key The array index or property name to sort by. 116 * @param string $type Sort type. 'alpha' for alphabetical, 'num' 117 * for numeric. Default: 'alpha'. 118 * @param bool $preserve_keys Whether to keep the keys or not. 119 * 115 120 * @return array $items The sorted array. 116 121 */ 117 function bp_sort_by_key( $items, $key, $type = 'alpha' ) { 118 usort( $items, array( new BP_Core_Sort_By_Key_Callback( $key, $type ), 'sort_callback' ) ); 122 function bp_sort_by_key( $items, $key, $type = 'alpha', $preserve_keys = false ) { 123 if ( true === $preserve_keys ) { 124 uasort( $items, array( new BP_Core_Sort_By_Key_Callback( $key, $type ), 'sort_callback' ) ); 125 } else { 126 usort( $items, array( new BP_Core_Sort_By_Key_Callback( $key, $type ), 'sort_callback' ) ); 127 } 119 128 120 129 return $items; -
trunk/tests/phpunit/testcases/core/functions.php
r11019 r11082 427 427 428 428 $this->assertEquals( $expected, bp_alpha_sort_by_key( $items, 'name' ) ); 429 } 430 431 /** 432 * @group bp_sort_by_key 433 */ 434 public function test_bp_sort_by_key_arrays_num_preserve_keys() { 435 $items = array( 436 'p' => array( 437 'foo' => 'bar', 438 'value' => 5, 439 ), 440 'q' => array( 441 'foo' => 'bar', 442 'value' => 10, 443 ), 444 'r' => array( 445 'foo' => 'bar', 446 'value' => 1, 447 ), 448 ); 449 450 $expected = array( 451 'r' => array( 452 'foo' => 'bar', 453 'value' => 1, 454 ), 455 'p' => array( 456 'foo' => 'bar', 457 'value' => 5, 458 ), 459 'q' => array( 460 'foo' => 'bar', 461 'value' => 10, 462 ), 463 ); 464 465 $this->assertEquals( $expected, bp_sort_by_key( $items, 'value', 'num', true ) ); 466 } 467 468 /** 469 * @group bp_sort_by_key 470 */ 471 public function test_bp_sort_by_key_num_should_respect_0_preserve_keys() { 472 $items = array( 473 's' => array( 474 'foo' => 'bar', 475 'value' => 2, 476 ), 477 't' => array( 478 'foo' => 'bar', 479 'value' => 0, 480 ), 481 'u' => array( 482 'foo' => 'bar', 483 'value' => 4, 484 ), 485 ); 486 487 $expected = array( 488 't' => array( 489 'foo' => 'bar', 490 'value' => 0, 491 ), 492 's' => array( 493 'foo' => 'bar', 494 'value' => 2, 495 ), 496 'u' => array( 497 'foo' => 'bar', 498 'value' => 4, 499 ), 500 ); 501 502 $this->assertEquals( $expected, bp_sort_by_key( $items, 'value', 'num', true ) ); 429 503 } 430 504
Note: See TracChangeset
for help on using the changeset viewer.