diff options
author | Stig Thormodsrud <stig@vyatta.com> | 2009-11-01 11:49:00 -0800 |
---|---|---|
committer | Stig Thormodsrud <stig@vyatta.com> | 2009-11-01 11:49:00 -0800 |
commit | 20512f3b1ce6fc6464b14f49b113fe010c37d375 (patch) | |
tree | ba72e6bd3b791b3a37f2b62cbcc71c656e856196 /lib | |
parent | 664bb2cd4f4cd1a9597cfb32962a582b0f56e39a (diff) | |
download | vyatta-cfg-20512f3b1ce6fc6464b14f49b113fe010c37d375.tar.gz vyatta-cfg-20512f3b1ce6fc6464b14f49b113fe010c37d375.zip |
Add natural order sort.
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/Vyatta/Keepalived.pm | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/lib/Vyatta/Keepalived.pm b/lib/Vyatta/Keepalived.pm index 13966c5..6d6a153 100755 --- a/lib/Vyatta/Keepalived.pm +++ b/lib/Vyatta/Keepalived.pm @@ -125,21 +125,56 @@ sub get_master_file { return $file; } +sub alphanum_split { + my ($str) = @_; + my @list = split m/(?=(?<=\D)\d|(?<=\d)\D)/, $str; + return @list; +} + +sub natural_order { + my ($a, $b) = @_; + my @a = alphanum_split($a); + my @b = alphanum_split($b); + + while (@a && @b) { + my $a_seg = shift @a; + my $b_seg = shift @b; + my $val; + if (($a_seg =~ /\d/) && ($b_seg =~ /\d/)) { + $val = $a_seg <=> $b_seg; + } elsif (($a_seg eq '.') && ($b_seg eq '_')) { + return 1; + } else { + $val = $a_seg cmp $b_seg; + } + if ($val != 0) { + return $val; + } + } + return @a <=> @b; +} + +sub intf_sort { + my @a = @_; + my @new_a = sort { natural_order($a,$b) } @a; + return @new_a; +} + sub get_state_files { my ($intf, $group) = @_; - # todo: fix sorting for ethX > 9 my @state_files; my $LS; if ($group eq "all") { - open($LS,"ls $state_dir |grep '^vrrpd_$intf.*\.state\$' | sort |"); + open($LS,"ls $state_dir |grep '^vrrpd_$intf.*\.state\$' |"); } else { my $intf_group = $intf . "_" . $group . ".state"; open($LS, - "ls $state_dir |grep '^vrrpd_$intf_group\$' | sort |"); + "ls $state_dir |grep '^vrrpd_$intf_group\$' |"); } @state_files = <$LS>; close($LS); + @state_files = intf_sort(@state_files); foreach my $i (0 .. $#state_files) { $state_files[$i] = "$state_dir/$state_files[$i]"; } |