Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/26/2014 06:21:42 PM (7 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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.