]> git.proxmox.com Git - pve-common.git/commitdiff
cli: print_text_table: correctly handle sort_key 0
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 25 Jul 2018 12:55:30 +0000 (14:55 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 26 Jul 2018 09:08:52 +0000 (11:08 +0200)
src/PVE/CLIFormatter.pm

index 22cd8f2378c803bbc977646f1ff8468b62dc89fc..8bb42ca15e044fa1e899a0398c15ea3fe74d1164 100644 (file)
@@ -132,7 +132,7 @@ sub data_to_text {
 # $returnprops -json schema property description
 # $props_to_print - ordered list of properties to print
 # $options
 # $returnprops -json schema property description
 # $props_to_print - ordered list of properties to print
 # $options
-# - sort_key: can be used to sort after a column, if it isn't set we sort
+# - sort_key: can be used to sort after a specific column, if it isn't set we sort
 #   after the leftmost column (with no undef value in $data) this can be
 #   turned off by passing 0 as sort_key
 # - noborder: print without asciiart border
 #   after the leftmost column (with no undef value in $data) this can be
 #   turned off by passing 0 as sort_key
 # - noborder: print without asciiart border
@@ -150,11 +150,9 @@ sub print_text_table {
     my $utf8 = $options->{utf8};
     my $encoding = $options->{encoding} // 'UTF-8';
 
     my $utf8 = $options->{utf8};
     my $encoding = $options->{encoding} // 'UTF-8';
 
-    if (!defined($sort_key) || $sort_key eq 0) {
-       $sort_key = $props_to_print->[0];
-    }
+    $sort_key //= $props_to_print->[0];
 
 
-    if (defined($sort_key)) {
+    if (defined($sort_key) && $sort_key ne 0) {
        my $type = $returnprops->{$sort_key}->{type} // 'string';
        if ($type eq 'integer' || $type eq 'number') {
            @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
        my $type = $returnprops->{$sort_key}->{type} // 'string';
        if ($type eq 'integer' || $type eq 'number') {
            @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;