Sorting an array

B

Bob Van Der Ploeg

How can I get perl to sort an array based on the second value inthe array?
example

@array=(201,1,635,4,987,3,407,2);
^ ^ ^ ^

Result I want is (201,1,407,2,987,3,635,4);

Would this be easier to sort if it were a multi-demsional array?

@array=([201,1],[635,4],[987,3],[407,2])

If so how do I sort a multi-demensional array?

Thanks,
Bob
 
A

Andreas Kahari

How can I get perl to sort an array based on the second value inthe array?
example

@array=(201,1,635,4,987,3,407,2);
^ ^ ^ ^

Result I want is (201,1,407,2,987,3,635,4);

Would this be easier to sort if it were a multi-demsional array?

@array=([201,1],[635,4],[987,3],[407,2])

Yes it would be. If they are to be kept in pairs, then keep
them in pairs.
If so how do I sort a multi-demensional array?

my @sorted = sort { $a->[1] <=> $b->[1] } @array;

Swap $a and $b if you want them sorted in the oher direction.
Replace [1] with [0] if you want to sort on the first value of
each pair.

Read the documentation about the sort function.
 
S

Sean O'Dwyer

Bob Van Der Ploeg said:
How can I get perl to sort an array based on the second value inthe array?
example
@array=(201,1,635,4,987,3,407,2);
Result I want is (201,1,407,2,987,3,635,4);

How about splitting the array's two parts into a hash...

$hashname{$secondnumber} = $firstnumber;
@sorted = sort(keys(%hashname));
foreach $num (@sorted) {print $num." = ".$hashname{$num};}

....this would work if you only had one occurrence of each second number
in @array. There's probably a more efficient way, but that's what occurs
to me at my level of skill.

Sean
 
R

rs

Bob said:
How can I get perl to sort an array based on the second value inthe array?
example

@array=(201,1,635,4,987,3,407,2);
^ ^ ^ ^

Result I want is (201,1,407,2,987,3,635,4);

Would this be easier to sort if it were a multi-demsional array?

@array=([201,1],[635,4],[987,3],[407,2])

If so how do I sort a multi-demensional array?
You can turn the array into a hash sort the hash and make an array again.
Since you should sort the keys and not the values and the order in your
aray is value, key,... just revers the array :

@array=(201,1,635,4,987,3,407,2);
my %hash = reverse @array;
my @sorted_array = map { $hash{$_}, $_ } sort keys %hash;
print join (',', @array), "\n";
print join (',', @sorted_array), "\n";

gives :
201,1,635,4,987,3,407,2
201,1,407,2,987,3,635,4

Greetings Rolf
 
A

Anno Siegel

Bob Van Der Ploeg said:
How can I get perl to sort an array based on the second value inthe array?
example

That sentence takes a lot of benign interpretation to make sense of.
@array=(201,1,635,4,987,3,407,2);
^ ^ ^ ^

Marking text only works with a fixed width font. You are not using one,
the "^" appear nowhere near they should be.
Result I want is (201,1,407,2,987,3,635,4);

So you want *pairs* of elements sorted according to the value of the
first element?
Would this be easier to sort if it were a multi-demsional array?

@array=([201,1],[635,4],[987,3],[407,2])

It would. Untested:

map @$_, sort { $a->[ 0] <=> $b->[ 0] } @array;

The "map" is there to put the list of pairs back into flat format.
Leave it out if you want to keep the pairs.
If so how do I sort a multi-demensional array?

Sorting is simple, the harder part is making the array of pairs. That
requires dealing with an error condition (odd number of elements given)
for one thing.

It is easier to use a hash, since transformation of a list of pairs
to a hash is built into Perl, error checking and all. Also untested:

my %h = @array;
map { ($_, $h{ $_}) } sort { $a <=> $b } keys %h;


Anno
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

How can I get perl to sort an array based on the second value inthe array?
example

@array=(201,1,635,4,987,3,407,2);
Result I want is (201,1,407,2,987,3,635,4);

Would this be easier to sort if it were a multi-demsional array?

Yes, but it *can* be done with your flat array:

my @sorted_idx = sort {
# we want ODD indices, so this turns 2 into 3, and 3 stays 3
my ($i, $j) = ($a | 1, $b | 1);

# if we were dealing with a pair like (201, 1)
# then compare the INDICES ($a and $b).
# if $i != $j, then we compare the VALUES (1, 2, 3, 4, ...)
$i == $j ? ($a <=> $b) : ($array[$i] <=> $array[$j]);
} @array;

my @sorted = @array[@sorted_idx];

But I don't suggest this. I'd use the array of array-refs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top