in reply to Sort array according to a value in each element?
A GRT will do it.
#! perl -slw use strict; my @array = split '\n', <<'EOA'; Item1 - 2 foo, 2 bar Item2 - 0 foo, 1 bar Item3 - 1 foo, 3 bar Item4 - 1 foo, 2 bar EOA my @sorted = map{ substr $_, 5; } sort map{ sprintf '%05d%s', $_ =~ m[,\s+(\d+)], $_; } @array; print for @sorted; __END__ Item2 - 0 foo, 1 bar Item1 - 2 foo, 2 bar Item4 - 1 foo, 2 bar Item3 - 1 foo, 3 bar
Update: Limbic~Region pointed out that you wanted descending not ascending. so substitute sort { $b cmp $a } for sort or use reverse on the results.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Sort array according to a value in each element?
by McMahon (Chaplain) on May 24, 2004 at 18:44 UTC | |
by Roy Johnson (Monsignor) on May 24, 2004 at 22:07 UTC |