Sorting Arrays by one element

They have: 568 posts

Joined: Nov 1999

i've been trying to do this for a long time now but i can't seem to find out how.

lets say i have an array called @test
in the array i have 3 elements, they contain

name1|100
name2|353
name3|89

i need to split the number from the name and then sort the numbers. But then i need to put the correct number with the correct name
so it would be

name2|353
name1|100
name3|89

any ideas on how i could do this?

They have: 161 posts

Joined: Dec 1999

Ugh, why do so many people use the pipe sign as an element separator? Sigh...

Anyway, here's how I'd do it, using a Schwartzian Transform (read "Effective Perl Programming" to find out more about these time saving structures):

Code Sample:

@sorted =
map { join "|", @$_ }
sort { $b->[1] <=> $a->[1] }
map { [ split /\|/ ] }
@list;

To learn about each of the individual segments, read up on: map(), sort(), split(), join(), and the perlref documentation for information on references.

------------------
--
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve

They have: 568 posts

Joined: Nov 1999

thanks a lot, that worked great

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.