[pve-devel] [PATCH cd-builder 1/2] fix #1999: sort listsnapshot output by snaptime

Thomas Lamprecht t.lamprecht at proxmox.com
Wed May 8 10:54:02 CEST 2019


On 5/3/19 11:48 AM, Rhonda D'Vine wrote:
> Signed-off-by: Rhonda D'Vine <rhonda at proxmox.com>
> ---
>  PVE/CLI/qm.pm | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
> index cea2549..fc06fa5 100755
> --- a/PVE/CLI/qm.pm
> +++ b/PVE/CLI/qm.pm
> @@ -922,7 +922,7 @@ our $cmddef = {
>      listsnapshot => [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node => $nodename },
>  		    sub {
>  			my $res = shift;
> -			foreach my $e (@$res) {
> +			foreach my $e (sort { $a->{snaptime} ? ( $b->{snaptime} ? ($a->{snaptime} <=> $b->{snaptime}) : -1 ) : 1 } @$res) {

maybe moving this to a coderef makes it looking a bit better? Something like:

my $time_cmp = sub {
    my ($a, $b) = $@;
	ret1urn +1 if !$a;
	return -1 if !$b;
	return $a <=> $b;
}

foreach my $e (sort { $time_cmp->($a->{snaptime}, $b->{snaptime}) } @$res) {


or just pass $a, $b and to the hash key access in the cmp method.
IMO, better to read as nested trenary operations..

>  			    my $headline = $e->{description} || 'no-description';
>  			    $headline =~ s/\n.*//sg;
>  			    my $parent = $e->{parent} // 'no-parent';
> 





More information about the pve-devel mailing list