]> git.proxmox.com Git - pve-common.git/commitdiff
PVE::CLIHandler::print_text_table - fix sorting
authorStoiko Ivanov <s.ivanov@proxmox.com>
Fri, 22 Jun 2018 18:21:06 +0000 (20:21 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 25 Jun 2018 10:09:14 +0000 (12:09 +0200)
Sort only if at least one column contains no undefined values.
Sort according to the left-most such column.

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
src/PVE/CLIHandler.pm

index 5f3927dd353326f5a739d42f486bc22715e80093..b04326f4ba2cf322b520761c3580485e240b53d4 100644 (file)
@@ -449,10 +449,11 @@ sub data_to_text {
 #            the last column will never be cutoff
 # 'default' - optional default value for the column
 # formatopts element order defines column order (left to right)
+# sorts the output according to the leftmost column not containing any undef
 sub print_text_table {
     my ($formatopts, $data) = @_;
 
-    my ($formatstring, @keys, @titles, %cutoffs, %defaults);
+    my ($formatstring, @keys, @titles, %cutoffs, %defaults, $sort_key);
     my $last_col = $formatopts->[$#{$formatopts}];
 
     foreach my $col ( @$formatopts ) {
@@ -467,11 +468,14 @@ sub print_text_table {
        my $titlelen = length($title);
 
        my $longest = $titlelen;
+       my $sortable = 1;
        foreach my $entry (@$data) {
            my $len = length(data_to_text($entry->{$key})) // 0;
            $longest = $len if $len > $longest;
+           $sortable = 0 if !defined($entry->{$key});
        }
 
+       $sort_key //= $key if $sortable;
        $cutoff = (defined($cutoff) && $cutoff < $longest) ? $cutoff : $longest;
        $cutoffs{$key} = $cutoff;
 
@@ -485,7 +489,10 @@ sub print_text_table {
 
     printf $formatstring, @titles;
 
-    foreach my $entry (sort { $a->{$keys[0]} cmp $b->{$keys[0]} } @$data) {
+    if (defined($sort_key)){
+       @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;
+    }
+    foreach my $entry (@$data) {
         printf $formatstring, map { substr((data_to_text($entry->{$_}) // $defaults{$_}), 0 , $cutoffs{$_}) } @keys;
     }
 }