Dual Sorting

GIDustin

Emperor
Joined
Nov 11, 2001
Messages
1,392
Location
Spearfish, SD
I need to sort an Array by one thing, then another.

If you need further details, I can give them to you, but it is kindof hard to explain.

I want to sort all my units, by their type (land, sea, air), but I dont want them in alphabeticall order (air, land, sea), so I made a hash:

$Hash{'Air'}=3;
$Hash{'Land'}=1;
$Hash{'Sea'}=2;

Units are put in another hash, like so:

$Units{'APC'}{'Type'}=Land;
$Units{'Tank'}{'Type'}=Land;
......
$Units{'Caraval'}{'Type'}=Sea;

to sort by type, you would do:
@AllUnits = keys %Units;
@AllUnits = sort {$Hash{$Units{$a}{'Type'}} <=> $Hash{$Units{$b}{'Type'}}} @AllUnits;

That sorts them by type, but they are not in alphabetical order within those types.

Hopefully I explained that correctly,

GIDustin
 
Maybe I should state that I am doing this in Perl, that might help, eh?

GIDustin
 
I figured out a way around it. It requires another sub, but I got the job done.

GIDustin
 
Back
Top Bottom