Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make fields sortable #142

Merged
merged 9 commits into from
Oct 27, 2017
Merged

Make fields sortable #142

merged 9 commits into from
Oct 27, 2017

Conversation

Sidsector9
Copy link
Member

@Sidsector9 Sidsector9 commented Oct 10, 2017

See #36

@Sidsector9 Sidsector9 requested a review from a team October 11, 2017 09:38
@@ -42,21 +42,39 @@ public function __construct( &$assoc_args, $fields = null, $prefix = false ) {
*
* @param array $items
*/
public function display_items( $items, $include_total = true ) {
public function display_items( $order, $orderby, $items, $include_total = true ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing this argument order is a backwards compatibility break (however unlikely it may be). Can we add the arguments to the end?

@@ -70,6 +70,13 @@ class Command {
*
* [--format=<format>]
* : Render output in a particular format.
*
* [--order=<order>]
* : Ascending or Descending order. ASC|DESC.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the YAML doc annotation to indicate available options?

* : Ascending or Descending order. ASC|DESC.
*
* [--orderby=<orderby>]
* : Order by fields.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the default order?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default order is ASC and there is no default orderby

$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', null );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this change the default display?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it does not @danielbachhuber


if ( $orderby ) {
usort( $items, function( $a, $b ) use ( $order, $orderby ) {
if ( 'ASC' === $order ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having two separate code paths for the two different sort orders, you could also swap them as needed, and then have 1 unified code path after that.

Something like this:

usort( $items, function( $a, $b ) use ( $order, $orderby ) {
   list( $first, $second ) = 'ASC' === $order ? array( $a, $b ) : array( $b, $a );

   if ( is_numeric( $first->$orderby ) && is_numeric( $second->$orderby ) ) {
      return $this->compare_float( $first->$orderby, $second->$orderby );
   }

   return strcmp( $first->$orderby, $second->$orderby );
});

@danielbachhuber
Copy link
Member

@Sidsector9 Planning to wrap this pull request up, or would you like help getting it over the finish line?

@Sidsector9
Copy link
Member Author

@danielbachhuber Give me a day more, I'll finish this

@schlessera schlessera dismissed danielbachhuber’s stale review October 27, 2017 05:12

Change requested was addressed.

@schlessera schlessera added this to the 0.4.0 milestone Oct 27, 2017
@schlessera schlessera merged commit cc92f5b into wp-cli:master Oct 27, 2017
@schlessera schlessera changed the title GH#36 Add sort feature by field type and orderby Make fields sortable Oct 27, 2017
danielbachhuber pushed a commit that referenced this pull request Nov 18, 2022
GH#36 Add sort feature by field type and orderby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants