Homerhrdz has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks:

I still have a few questions on sorting I was wondering if it is possible to sort by value so for example something like this

@sorted = sort { ("Dew") } @not_sorted;

I'm trying to sort a column but just wanted to know if something like the code i put above is possible. I have been reading all over the place and can not find the answer

Thank you so much for the help

Homer

Replies are listed 'Best First'.
Re: Sorting by value??
by kennethk (Abbot) on Jun 15, 2010 at 15:01 UTC
    As long as you can code the logic, you can sort by anything. See sort and How do I sort an array by (anything)?. If these sources are unclear, provide a list of inputs, a desired output order and possibly a rough sketch of your sorting logic so we can provide more guidance (see How do I post a question effectively?). If you specifically want to float a given value to the top of a list (that's the only logical conclusion I can draw from your post), you might use something like:

    use strict; use warnings; my @not_sorted = qw(you hew dew slew); my $value = "dew"; my @sorted = sort { ($a eq $value) ? ($b eq $value) ? 0 : -1 : ($b eq $value) ? 1 : 0; } @not_sorted; print join "\n", @sorted;
    A reply falls below the community's threshold of quality. You may see it by logging in.