On Fri, Jul 24, 2009 at 00:14, sys adm<sys...@computermail.net> wrote:
> Hi,
>
> When I got a word list, I want it to be sorted with special order.
> for example, I got this array:
>
> ("dog","is","a","there");
>
> I want the sorted result is:
>
> ("there","is","a","dog");
>
> How to code it? Thank you.
snip

You need to some way to map the list to be sorted into something that
can be sorted.  When you hear the phrase map X into Y, you should
think of a hash:

#!/usr/bin/perl

use strict;
use warnings;

my %sort_order = (
        there => 1,
        is    => 2,
        a     => 3,
        dog   => 4,
);

my @list = qw/dog is a there/;

my @sorted = sort  { $sort_order{$a} <=> $sort_order{$b} } @list;

print "list:\n", (map { "\t$_\n" } @list),
        "sorted\n", map { "\t$_\n" } @sorted;


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to