Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
Ever wanted to sort on a computed value or a certain column in a string within an array. It can get pretty tricky to do right. You only want to compute the value you're sorting on once for each array element not every time you do a comparison throughout the sort.

One key function you'll want to know how to use is map. Map allows you to apply a change to a list and get back a new list with the changes.

There are two ways you can use the map function;
#map BLOCK, LIST #or something like @revwords=map{ @letters=split(//); @revletters=reverse(@letters); join(' ',@revletters); } @words; #or #map EXPR, LIST @asciival=map(ord,@letters);
The first example takes a list of words and returns a list of each of those words with the letters reversed. This is done by splitting them, then reversing the resulting array, and then returning the joined array which is the word reversed. The second example takes an array of letters and returns a list of their ascii values;

Now say we want to sort an array that contains 3 columns of numbers separated by spaces. We want to sort this array on the sum of these three numbers.

The first thing we want to do is map the original array to an array with the calculated sums, as well as the original values in it.
@origarray=('0 1 2','0 0 0','-3 -2 -1','10 11 12'); @sortedarray= map{$_->[0]} sort{$a->[1] <=> $b->[1]} map{ my @cols=split(/\s+/); my $sum=$cols[0]+$cols[1]+$cols[2]; [$_,$sum]; } @origarray; foreach(@sortedarray){ print "$_\n"; }
We start by doing the basic transform within the innermost map. The current list value comes in as the default variable($_), then we split the columns, add them up, and return [$_, $sum] which contains the original value followed by the computed value for sorting on. We then sort numerically on this computed value. And then use map so the array only contains the original stuff.

This whole process is known as the Schwartzian Transform.
Here's a template for doing complex sorting
@sortedarray= map{$_->[0]} # only use one of these sort lines sort{$a->[1] <=> $b->[1]} # this one for numerical sorting sort{$a->[1] cmp $b->[1]} # this one for textual sorting map{ #calculate $computedsortval [$_,$computedsortval]; } @origarray;
This should hopefully give you a better understanding of what is going on when you need to do a complex sort, and hopefully allow you to do it right with very little hassle.

In reply to Complex sorting by vroom

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.