in reply to Sorting array with multiple values per keys
In addition, is it bad to use the array to put strings with two 'artifical' elements. Is there another way to do it more efficient
There are definitely more efficient ways, and there are documents like perldsc and perllol that describe them.
The answer to your first question would probably involve transforming your strings into a proper data structure, sorting them, and transforming them back:
my @Array; $Array[0] = "1,5"; $Array[3] = "3,9"; $Array[2] = "3,4"; $Array[1] = "6,2"; $Array[4] = "8,1"; @Array = map { join ',', @$_; } sort { $a->[0] <=> $b->[0] or $b->[1] <=> $a->[1] } map { [split /,/] } @Array; use Data::Dumper; print Dumper \@Array;
(Update:) Now, here's a more efficient / elegant way to do this:
my @Array = ( [1, 5], [6, 2], [3, 4], [3, 9], [8, 1], ); @Array = sort { $a->[0] <=> $b->[0] or $b->[1] <=> $a->[1] } @Array; use Data::Dumper; print Dumper \@Array;
You see that the sort block is the same.
(This uses a list of lists, while kyle's approach below uses a list of hashes, which enables you to use the x and y that you hinted at.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting array with multiple values per keys
by ikegami (Patriarch) on Aug 12, 2008 at 23:26 UTC |