Skip to:
Content

BuddyPress.org

Changeset 9191


Ignore:
Timestamp:
11/26/2014 06:21:42 PM (10 years ago)
Author:
boonebgorges
Message:

Introduce bp_sort_by_key().

A companion for bp_alpha_sort_by_key(), this function allows arrays of
objects or associative arrays to be sorted according to one of their keys/
properties - a kinda-sorta version of wp_list_pluck() for sorting. Supports
numeric as well as alphabetical sorts.

bp_alpha_sort_by_key() is converted to a wrapper of the new function.

Fixes #6047. See #5669.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-functions.php

    r9180 r9191  
    9494
    9595/**
    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.
    10197 *
    10298 * The main purpose for this function is so that you can avoid having to create
    10399 * your own awkward callback function for usort().
    104100 *
    105  * @since BuddyPress (1.9.0)
    106  *
    107  * @param array $items The array to be sorted. Its constituent items can be
    108  *        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 */
     108function bp_sort_by_key( $items, $key, $type = 'alpha' ) {
    113109    usort( $items, create_function( '$a, $b', '
    114110        $values = array( 0 => false, 1 => false, );
     
    123119
    124120        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
    126127            if ( 0 > $cmp ) {
    127128                $retval = -1;
     
    138139
    139140    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 */
     156function bp_alpha_sort_by_key( $items, $key ) {
     157    return bp_sort_by_key( $items, $key, 'alpha' );
    140158}
    141159
  • trunk/tests/phpunit/testcases/core/functions.php

    r9180 r9191  
    188188    }
    189189
     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    }
    190257    /**
    191258     * @group bp_alpha_sort_by_key
Note: See TracChangeset for help on using the changeset viewer.