Skip to content

Commit

Permalink
fixed and refactored sorted code: now the sort order for equal sort f…
Browse files Browse the repository at this point in the history
…ields will not be violated
  • Loading branch information
Fellan-91 committed May 14, 2024
1 parent 1599b41 commit cfdd706
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions classes/class-get-portfolio.php
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,23 @@ private static function get_rand_seed_session() {
return self::$rand_seed_session;
}

/**
* Sort associative array by custom field.
*
* @param array $array - Sortable array.
* @param string $field - Array field by which sorting will take place.
* @param string $order - Sorting order (asc and desc).
* @return void
*/
public static function sort_by_field( &$array, $field, $order = 'desc' ) {
usort(
$array,
function ( $a, $b ) use ( $field, $order ) {
return 'desc' === $order ? $b[ $field ] <=> $a[ $field ] : $a[ $field ] <=> $b[ $field ];
}
);
}

/**
* Get query params array.
*
Expand Down Expand Up @@ -1366,18 +1383,16 @@ public static function get_query_params( $options, $for_filter = false, $layout_
*/
$non_empty_images = array();
$empty_images = array();
$sort_keys = array();

foreach ( $images as $image ) {
if ( empty( $image[ $custom_order ] ) ) {
$empty_images[] = $image;
} else {
$non_empty_images[] = $image;
$sort_keys[] = $image[ $custom_order ];
}
}

array_multisort( $sort_keys, 'desc' === $custom_order_direction ? SORT_DESC : SORT_ASC, $non_empty_images );
self::sort_by_field( $non_empty_images, $custom_order, $custom_order_direction );

$images = array_merge( $non_empty_images, $empty_images );
break;
Expand Down

0 comments on commit cfdd706

Please sign in to comment.