I am looking for some better methods like sort_on_fields(1,2,4,6,9) rather than sort { $a1 <=> $b1 or .. }

Assuming that you mean that the sort field numbers cannot be hard coded as in the FAQ, then something like this does the trick:

#! perl -slw use strict; use Data::Dump qw[ pp ]; sub sortOnFields{ my $r; $r = $a->[ $_ ] <=> $b->[ $_ ] and return $r for @_; return 0; } my @data = map[ map int( rand 10 ), 1 .. 10 ], 1 .. 20; @data = sort{ sortOnFields( 0,2,4,6 ) } @data; pp \@data; __END__ C:\test>908993 [ [0, 9, 1, 9, 0, 8, 7, 5, 9, 5], [0, 2, 9, 7, 5, 9, 1, 8, 8, 7], [1, 0, 2, 4, 1, 8, 3, 4, 6, 4], [1, 6, 6, 7, 6, 0, 3, 1, 8, 8], [1, 8, 8, 0, 4, 5, 0, 8, 8, 3], [2, 5, 7, 6, 5, 4, 6, 8, 2, 0], [3, 2, 0, 4, 2, 5, 1, 1, 6, 6], [3, 2, 1, 2, 2, 4, 7, 9, 8, 5], [3, 1, 6, 9, 0, 3, 0, 5, 6, 4], [5, 5, 3, 8, 1, 1, 0, 5, 7, 0], [5, 1, 5, 7, 9, 8, 3, 3, 8, 7], [5, 6, 6, 4, 2, 9, 7, 7, 2, 4], [6, 9, 1, 6, 5, 5, 0, 4, 0, 1], [9, 3, 0, 3, 0, 1, 2, 2, 7, 0], [9, 2, 0, 6, 8, 6, 0, 6, 5, 0], [9, 7, 0, 3, 8, 2, 2, 3, 7, 0], [9, 1, 2, 7, 2, 9, 6, 7, 3, 9], [9, 5, 2, 0, 2, 4, 8, 0, 7, 1], [9, 9, 2, 2, 8, 5, 8, 3, 3, 0], [9, 1, 4, 4, 5, 0, 2, 6, 8, 8], ]

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Sorting by Multiple Columns by BrowserUk
in thread Sorting by Multiple Columns by aartist

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.