Using perl Syntax Highlighting
@nodos = (
{ id => 17, size => 300, keys => 2, cmp => 'keys' },
{ id => 14, size => 104, keys => 9, cmp => 'size' },
{ id => 31, size => 2045, keys => 43, cmp => 'keys' },
{ id => 28, size => 6, keys => 0, cmp => 'id' },
);
{ id => 17, size => 300, keys => 2, cmp => 'keys' },
{ id => 14, size => 104, keys => 9, cmp => 'size' },
{ id => 31, size => 2045, keys => 43, cmp => 'keys' },
{ id => 28, size => 6, keys => 0, cmp => 'id' },
);
Coloreado en 0.003 segundos, usando GeSHi 1.0.8.4
La idea es ordenar las tres listas según el valor de 'cmp' en cada una de ellas a través de la función sort. Lo he intentado, pero nada. Algo de ayuda por favor.
Hasta ahora he llegado a esto:
Using perl Syntax Highlighting
use strict;
sub sort_nodes {
my @init = @_;
my @keys_values;
die "you must define the array!\n" unless defined @init;
my $count;
foreach(@init) {
push @keys_values, $init[$count]{'cmp'};
$count++;
}
my @sorted = sort { $a cmp $b } @keys_values;
my $elements = @sorted;
my @final;
for(my $i;$i<=$elements;$i++) {
for(my $j;$j<=$elements;$j++) {
if ($sorted[$i] eq $init[$j]{'cmp'}) {
push @final, $init[$j];
last;
}
}
}
return @final;
}
my @nodes = (
{ id => 17, size => 300, keys => 2, cmp => 'keys' },
{ id => 14, size => 104, keys => 9, cmp => 'size' },
{ id => 31, size => 2045, keys => 43, cmp => 'keys' },
{ id => 28, size => 6, keys => 0, cmp => 'id' },
);
my @sorted_nodes = sort_nodes(@nodes);
sub sort_nodes {
my @init = @_;
my @keys_values;
die "you must define the array!\n" unless defined @init;
my $count;
foreach(@init) {
push @keys_values, $init[$count]{'cmp'};
$count++;
}
my @sorted = sort { $a cmp $b } @keys_values;
my $elements = @sorted;
my @final;
for(my $i;$i<=$elements;$i++) {
for(my $j;$j<=$elements;$j++) {
if ($sorted[$i] eq $init[$j]{'cmp'}) {
push @final, $init[$j];
last;
}
}
}
return @final;
}
my @nodes = (
{ id => 17, size => 300, keys => 2, cmp => 'keys' },
{ id => 14, size => 104, keys => 9, cmp => 'size' },
{ id => 31, size => 2045, keys => 43, cmp => 'keys' },
{ id => 28, size => 6, keys => 0, cmp => 'id' },
);
my @sorted_nodes = sort_nodes(@nodes);
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4