From: Stoiko Ivanov Date: Fri, 22 Jun 2018 18:21:06 +0000 (+0200) Subject: PVE::CLIHandler::print_text_table - fix sorting X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=e05f7cdd0c5ed604d25701de3dfdc091bbf29ad1 PVE::CLIHandler::print_text_table - fix sorting Sort only if at least one column contains no undefined values. Sort according to the left-most such column. Signed-off-by: Stoiko Ivanov --- diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm index 5f3927d..b04326f 100644 --- a/src/PVE/CLIHandler.pm +++ b/src/PVE/CLIHandler.pm @@ -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; } }