If each hash referent in an array contains information about a unique platform and if each platform is uniquely identified by, say, a  'platformid' => ... (or some other such) number, it may be enough to sort just by these unique platform identifier numbers:

my @array1 = ( {'platformid' => '22', ... }, {'platformid' => '100', ... }, ..., ); my @ordered_array1 = sort { $a->{platformid} <=> $b->{platformid} }@ar +ray1; # ascending numeric sort ...; my $rc = Compare(\@ordered_array1, \@ordered_array2); ...;
Of course, if you have an array like
my @array1 = ( {'platformid' => '22', 'some' => 'stuff', ... }, {'platformid' => '100', ... }, ..., {'platformid' => '22', 'other' => 'things', ... }, ..., );
this is not going to work.

Note also that things like  'platformid' => '22' that seem to be numbers should be numerically compared with the  <=> operator. Lexical (string-wise) comparison of numbers with the  cmp operator will give strange results.

Another point is that if you really need to use an enormously complex comparison like

my @array3 = sort { $a->{platformid} cmp $b->{platformid} or $a->{da} cmp $b->{da} or $a->{ma} cmp $b->{ma} or $a->{os} cmp $b->{os} or $a->{cc} cmp $b->{cc} or $a->{objecttype} cmp $b->{objecttype} or $a->{host} cmp $b->{host} or $a->{size} cmp $b->{size} } @array1;
and use it in more than one place, then this comparison can and should be encapsulated in a sanity-saving function:
sub enormously_complex_compare { $a->{platformid} <=> $b->{platformid} # numeric? or $a->{da} cmp $b->{da} or $a->{ma} cmp $b->{ma} or $a->{os} cmp $b->{os} or $a->{cc} cmp $b->{cc} or $a->{objecttype} <=> $b->{objecttype} # numeric? or $a->{host} <=> $b->{host} # numeric? or $a->{size} <=> $b->{size} # ??? comparing array references ??? } my @array3 = sort enormously_complex_compare @array1; my @array4 = sort enormously_complex_compare @array2; ...
Note that the value of the  'size' key is an array reference, and comparing references as numbers (or strings) is problematic: exactly what is the point of this comparison?


Give a man a fish:  <%-{-{-{-<


In reply to Re: Compare complex perl data structures by AnomalousMonk
in thread Compare complex perl data structures by AnishaM

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.