Skip to:
Content

BuddyPress.org

Ticket #7238: 7238.1.patch

File 7238.1.patch, 3.3 KB (added by dcavins, 7 years ago)

Add $preserve_keys parameter to bp_sort_by_key().

  • src/bp-core/bp-core-functions.php

    diff --git src/bp-core/bp-core-functions.php src/bp-core/bp-core-functions.php
    index d3bcbde..c536ac2 100644
    function bp_core_get_table_prefix() { 
    108108 * your own awkward callback function for usort().
    109109 *
    110110 * @since 2.2.0
     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.
    111119 *
    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'.
    115120 * @return array $items The sorted array.
    116121 */
    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' ) );
     122function 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        }
    119128
    120129        return $items;
    121130}
  • tests/phpunit/testcases/core/functions.php

    diff --git tests/phpunit/testcases/core/functions.php tests/phpunit/testcases/core/functions.php
    index 64d6088..0c004d6 100644
    class BP_Tests_Core_Functions extends BP_UnitTestCase { 
    429429        }
    430430
    431431        /**
     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 ) );
     503        }
     504
     505        /**
    432506         * @group pagination
    433507         * @group bp_sanitize_pagination_arg
    434508         */