From 6c19ef10c2cb91fdfb242591e5b06b58317d9624 Mon Sep 17 00:00:00 2001 From: Sidsector9 Date: Tue, 10 Oct 2017 18:54:40 +0530 Subject: [PATCH] GH#36 Add sort feature by field type and orderby --- inc/class-command.php | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/inc/class-command.php b/inc/class-command.php index f2bf32f..d2bf3d6 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -70,6 +70,13 @@ class Command { * * [--format=] * : Render output in a particular format. + * + * [--order=] + * : Ascending or Descending order. ASC|DESC. + * + * [--orderby=] + * : Order by fields. + * * --- * default: table * options: @@ -86,6 +93,13 @@ class Command { $focus = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null ); + $order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' ); + $orderby = Utils\get_flag_value( $assoc_args, 'orderby' ); + + if ( Utils\get_flag_value( $assoc_args, 'fields' ) ) { + $set_fields = explode( ',', Utils\get_flag_value( $assoc_args, 'fields' ) ); + } + $valid_stages = array( 'bootstrap', 'main_query', 'template' ); if ( $focus && ( true !== $focus && ! in_array( $focus, $valid_stages, true ) ) ) { WP_CLI::error( 'Invalid stage. Must be one of ' . implode( ', ', $valid_stages ) . ', or use --all.' ); @@ -132,9 +146,44 @@ class Command { if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) { $loggers = self::shine_spotlight( $loggers, $metrics ); } + + if ( $orderby ) { + usort( $loggers, function( $a, $b ) use ( $order, $orderby ) { + if ( 'ASC' === $order ) { + if ( is_numeric( $a->$orderby ) && is_numeric( $b->$orderby ) ) { + return $this->compare_float( $a->$orderby, $b->$orderby ); + } else { + return strcmp( $a->$orderby, $b->$orderby ); + } + } elseif ( 'DESC' === $order ) { + if ( is_numeric( $b->$orderby ) && is_numeric( $a->$orderby ) ) { + return $this->compare_float( $b->$orderby, $a->$orderby ); + } else { + return strcmp( $b->$orderby, $a->$orderby ); + } + } + }); + } + $formatter->display_items( $loggers ); } + /** + * Function to compare floats. + * + * @param double $a Floating number. + * @param double $b Floating number. + */ + public function compare_float( $a, $b ) { + if ( abs( $a - $b ) < 0.00000001 ) { + return 0; + } elseif ( ( $a - $b ) < 0 ) { + return -1; + } else { + return 1; + } + } + /** * Profile key metrics for WordPress hooks (actions and filters). *