Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort array of arrays

Status
Not open for further replies.

kevin197

Programmer
Mar 21, 2002
88
GB
I'm trying to sort my array of arrays using the following code but it doesn't seam to be working. Any ideas why anyone?


# assign to our array, an array of array references
@AoA = (
[ "$price[0]", "$postage[0]", "$condition[0]", "$seller[0]" ],
[ "$price[1]", "$postage[1]", "$condition[1]", "$seller[1]" ],
[ "$price[2]", "$postage[2]", "$condition[2]", "$seller[2]" ],
);

@AoAsorted = sort { $a->[0] <=> $b->[0] } @AoA;

print "$AoAsorted[0]->[0]";
 
what's happening, when you say it doesn't appear to work, what are you expoecting?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Sorry, I should of said.

I want to sort in order of $price smallest to highest.

At the moment I just get the first $price out but it's in the order it went in and not sorted.
 
The sorting is OK:

Code:
use Data::Dumper;
    @AoA = (
       [ 2, 4, 'x', 'y' ],
       [ 3, 4, 'x', 'y' ],
       [ 1, 4, 'x', 'y' ],
    );
	 @AoAsorted = sort { $a->[0] <=> $b->[0] } @AoA;
print Dumper \@AoAsorted;

But in your code all you are printing at the end is the first element of the array:

Code:
print "$AoAsorted[0]->[0]";

So naturally that is all you are going to see.




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I was expecting it to print the smallest price out of $price[0] price[1] or price[2]

I need to sort by price so then the array with the lowest price is at the top in the array.

I thought that by sorting it it would basically moves the arrays up or down in the list.
 
I've got it working now with the following code


%data = ("$seller[0]" => "$price[0]",
"$seller[1]" => "$price[1]",
"$seller[2]" => "$price[2]",
"$seller[3]" => "$price[3]",
"$seller[4]" => "$price[4]");



my @sorted_keys = sort { $data{$a} <=> $data{$b} } keys %data;


This works great as I can call the smallest with

$line[75] = $data{$sorted_keys[0]};
$line[78] = $sorted_keys[0];


Now I want to add another piece of information to the hash to be something like this


%data = ("$seller[0]" => "$price[0]" => "$postage[0]",
"$seller[1]" => "$price[1]" => "$postage[0]",
"$seller[2]" => "$price[2]" => "$postage[0]",
"$seller[3]" => "$price[3]" => "$postage[0]",
"$seller[4]" => "$price[4]" => "$postage[0]");

How would I sort it now in the same way as before by price?

I just need to be able to pull out the postage for the one thats got the lowest price with something like

$line[78] = $sorted_keys[1];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top