Just to be clear, you have an array consisting of lines like the one you describe above? So your data could be constructed with something like the following?
my @Data = ( "1|someproject|active...", "2|somethingelse|finished..." );
If that is correct, then you can use something like the following to achieve your aim:
#!/usr/bin/perl -wT use strict; # position to sort by my $sortbyfield = 3; my @input = ( '1|2|1|4|5|6|7|8|9', '1|2|2|5|5|6|7|8|9', '1|2|0|1|5|6|7|8|9', '1|2|9|2|5|6|7|8|9', '1|2|8|3|5|6|7|8|9', '1|2|7|4|5|6|7|8|9', '1|2|6|7|5|6|7|8|9', ); # Schwartzian Transform my @output = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, getfieldvalue($_, $sortbyfield)] } @input; print join( "\n", @output), "\n"; sub getfieldvalue { my ($line, $fieldno) = @_; my @items = split /\|/, $line; return $items[$fieldno]; } __END__ returns (sorted by 3rd _position, arrays start at 0) 1|2|0|1|5|6|7|8|9 1|2|9|2|5|6|7|8|9 1|2|8|3|5|6|7|8|9 1|2|1|4|5|6|7|8|9 1|2|7|4|5|6|7|8|9 1|2|2|5|5|6|7|8|9 1|2|6|7|5|6|7|8|9
Please note that the above will only work for numeric input. You can change it to use string compare by replacing the <=> with cmp.

I would suggest thinking about what you're doing with this data, however. If you're going to be accessing bits of it more than just this time, then perhaps you should be using an array of hashes or at least and array of arrays? See perlreftut.

If you break this into an array of hashes:

my @fields = qw/id projname status submitdt assign_dt total complete_dt person dept closed_dt/; # Create my array of hashes. foreach (@Data) { my %hash; @hash{@fields} = split /\|/, $_; $_ = \%hash; }
You can access individual items as follows:
print "The id of the project in Data[0] is $Data[0]{id}\n";
This might make it easier to sort, too.
my @sorted = sort {$a->{projname} cmp $b->{projname}} @Data; # To get the same kind of format as before ... foreach (@sorted) { my %temp = %$_; print "@temp{@fields}\n"; }
As I said, it depends on how much manipulation is required on the input.

I hope this helps

jarich


In reply to Re: How to sort an array by jarich
in thread How to sort an array by cosmicsoup

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.