2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-05-29 17:23:08 +00:00

Introduce a natural compare func for sorting array and using it for rinv/rvital (#4307)

* Introduce a natural compare func for sorting array and using it for rinv/rvital

* 1, Remove the comments for previous codes 2,not using a new array variable there
This commit is contained in:
Bin Xu 2017-11-16 11:31:03 +08:00 committed by yangsong
parent 9f6c4310fb
commit 585d771ed1
2 changed files with 40 additions and 9 deletions

View File

@ -48,7 +48,7 @@ require xCAT::Version;
require DBI;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(genpassword runcmd3);
our @EXPORT_OK = qw(genpassword runcmd3 natural_sort_cmp);
# The functions that has been moved to TableUtils.pm
@ -4918,5 +4918,34 @@ sub acquire_lock_imageop {
return (0,$lock);
}
#--------------------------------------------------------------------------------
=head3 natural_sort_cmp
compare $left and $right by natrual.
=cut
#--------------------------------------------------------------------------------
sub natural_sort_cmp {
my $left = shift;
my $right = shift;
if( !($left =~ /\d+(\.\d+)?/) ) {
return $left cmp $right;
}
my $before = $`;
my $match = $&;
my $after = $';
if( !($right =~ /\d+(\.\d+)?/) ) {
return $left cmp $right;
}
if( $before eq $` ) {
if( $match == $& ) {
return natural_sort_cmp( $after, $' );
} else {
return $match <=> $&;
}
} else {
return $left cmp $right;
}
}
1;

View File

@ -33,6 +33,7 @@ use xCAT::SvrUtils;
use xCAT::GlobalDef;
use xCAT_monitoring::monitorctrl;
use POSIX qw(WNOHANG);
use xCAT::Utils qw/natural_sort_cmp/;
$::VERBOSE = 0;
# String constants for rbeacon states
@ -71,6 +72,10 @@ sub unsupported {
}
}
sub natural_sorter {
natural_sort_cmp( $a, $b );
}
#-------------------------------------------------------
=head3 handled_commands
@ -1887,12 +1892,10 @@ sub rinv_response {
}
}
}
# If sorted array has any contents, sort it and print it
# If sorted array has any contents, sort it naturally and print it
if (scalar @sorted_output > 0) {
# sort alpha, then numeric
my @sorted_output = grep {s/(^|\D)0+(\d)/$1$2/g,1} sort
grep {s/(\d+)/sprintf"%06.6d",$1/ge,1} @sorted_output;
foreach (@sorted_output) {
foreach (sort natural_sorter @sorted_output) {
#
# The firmware output requires the ID to be part of the string to sort correctly.
# Remove this ID from the output to the user
@ -2195,7 +2198,8 @@ sub rspconfig_response {
push @output, "BMC IP Source: $ipsrc";
} elsif ($opt eq "netmask") {
if ($address) {
my $decimal_mask = (2 ** $prefix - 1) << (32 - $prefix);
my $mask_shift = 32 - $prefix;
my $decimal_mask = (2 ** $prefix - 1) << $mask_shift;
my $netmask = join('.', unpack("C4", pack("N", $decimal_mask)));
push @output, "BMC Netmask: " . $netmask;
}
@ -2390,9 +2394,7 @@ sub rvitals_response {
# If sorted array has any contents, sort it and print it
if (scalar @sorted_output > 0) {
# Sort the output, alpha, then numeric
my @sorted_output = grep {s/(^|\D)0+(\d)/$1$2/g,1} sort
grep {s/(\d+)/sprintf"%06.6d",$1/ge,1} @sorted_output;
xCAT::SvrUtils::sendmsg("$_", $callback, $node) foreach (@sorted_output);
xCAT::SvrUtils::sendmsg("$_", $callback, $node) foreach (sort natural_sorter @sorted_output);
} else {
xCAT::SvrUtils::sendmsg("$::NO_ATTRIBUTES_RETURNED", $callback, $node);
}