Rinku Mahesh <mailto:[EMAIL PROTECTED]> wrote:
: If the above explaination is confusing I'm looking for a way : where every element of an array can be mapped to corresponding : element of another array and as a whole both the arrays require : a sorting w.r.t. 2nd array. M.J. Dominus wrote three slides about an Indirect Sort which you may find helpful. He's using two arrays of names. One for first names, one for last names. Basically, the idea is to sort the array indexes and then apply the sorted indexes to each array using an array slice. (I don't know if he's right about those APL folks.) http://perl.plover.com/yak/hw2/samples/slide003.html In your case, you need a numerical comparison (<=>) on both arrays as opposed to a string comparison (cmp). You also require that ties be sorted on the other array. That can be performed within the sort. One thing I like about sorting indexes is that you can save the sorts. use strict; use warnings; # Test data my @unique = ( 66, 23, 44, 11, 900, 1009 ); my @occ_count = ( 2, 77, 22, 2, 77, 29 ); # Sort in ascending order, ascending order # Use reverse for descending order, descending order my @ascending_indexes = sort { $occ_count[$a] <=> $occ_count[$b] || $unique[$a] <=> $unique[$b] } 0 .. $#occ_count; # Sort in descending order, ascending order # Use reverse for ascending order, descending order my @descending_indexes = sort { $occ_count[$b] <=> $occ_count[$a] || $unique[$a] <=> $unique[$b] } 0 .. $#occ_count; # Reports print "Ascending sort then ascending sort:\n\t"; printf '%5d', $_ foreach @occ_count[ @ascending_indexes ]; print "\n\t"; printf '%5d', $_ foreach @unique[ @ascending_indexes ]; print "\n\n"; print "Ascending sort then descending sort\n\t"; printf '%5d', $_ foreach @occ_count[ reverse @descending_indexes ]; print "\n\t"; printf '%5d', $_ foreach @unique[ reverse @descending_indexes ]; print "\n"; print "Descending sort then descending sort:\n\t"; printf '%5d', $_ foreach @occ_count[ reverse @ascending_indexes ]; print "\n\t"; printf '%5d', $_ foreach @unique[ reverse @ascending_indexes ]; print "\n\n"; print "Descending sort then ascending sort\n\t"; printf '%5d', $_ foreach @occ_count[ @descending_indexes ]; print "\n\t"; printf '%5d', $_ foreach @unique[ @descending_indexes ]; print "\n"; __END__ For larger arrays, you might want to transform one array first. HTH, Charles K. Clarkson -- Mobile Homes Specialist Free Market Advocate Web Programmer 254 968-8328 http://www.clarksonenergyhomes.com/ Don't tread on my bandwidth. Trim your posts. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>