Sorting Array of String Backward

Discussion in 'Perl Misc' started by nntp, Oct 13, 2004.

  1. nntp

    nntp Guest

    I have an array of strings I need to sort from backward, such as in this
    order.

    cba
    dba
    eba

    I perldoc -f sort, but I can not see it has this feature. Basically, if I
    can reverse the string, sort them, then rever them back will do the trick.
    The reverse function in perl only do on arrays not on strings.
     
    nntp, Oct 13, 2004
    #1
    1. Advertising

  2. nntp

    Paul Lalli Guest

    "nntp" <> wrote in message
    news:...
    > I have an array of strings I need to sort from backward, such as in

    this
    > order.
    >
    > cba
    > dba
    > eba


    These seem sorted pretty normally to me. How is this backwards?

    I shall assume for the remainder of the post that you mean you want a
    sorted order along the lines of

    abc bba cab ==> bba cab abc
    ie, sorted by last character first, then second to last character, etc.

    > I perldoc -f sort, but I can not see it has this feature. Basically,

    if I
    > can reverse the string, sort them, then rever them back will do the

    trick.
    > The reverse function in perl only do on arrays not on strings.


    What makes you believe that?
    perldoc -f reverse
    reverse LIST
    In list context, returns a list value consisting of
    the elements of LIST in the opposite order. In
    scalar context, concatenates the elements of LIST
    and returns a string value with all characters in
    the opposite order.

    $s = 'abc';
    $r = reverse $s;
    print "$r\n";
    __END__
    cba

    You pass sort a function that defines how you want to sort the elements.
    So you could simply do:
    my @sorted = sort { (reverse $a) cmp (reverse $b) } @unsorted

    However, you may also wish to consider using a Schwartzian Transform, to
    minimize the performance hit of reversing every string every time the
    sort subroutine is called:

    my @sorted =
    map $_->[0],
    sort { $a->[1] cmp $b->[1] }
    map [$_, scalar(reverse $_)],
    @unsorted;

    Paul Lalli
     
    Paul Lalli, Oct 13, 2004
    #2
    1. Advertising

  3. nntp wrote:
    > I have an array of strings I need to sort from backward, such as in
    > this order.
    >
    > cba
    > dba
    > eba
    >
    > I perldoc -f sort, but I can not see it has this feature.


    Well, no.
    sort() cannot possibly provide options for sorting in every imaginable way.
    That's why you can pass a comparison function of your own.

    > Basically,
    > if I can reverse the string, sort them, then rever them back will do
    > the trick. The reverse function in perl only do on arrays not on
    > strings.


    What?
    Were you fooled by the "LIST" in the header of the reverse() entry in
    perldoc?
    "reverse LIST"

    First you can always pass a list with one element.
    And second you should have continued to read:
    [...] In scalar context, concatenates
    the elements of LIST and returns a string value with all
    characters in the opposite order.


    The line
    print scalar reverse "foobar";
    prints a nice
    raboof
    for me.

    jue
     
    Jürgen Exner, Oct 13, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. paul
    Replies:
    6
    Views:
    370
  2. =?Utf-8?B?TUxpYmJ5?=

    Netscape - Backward compatibility testing

    =?Utf-8?B?TUxpYmJ5?=, Sep 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    634
    [MSFT]
    Sep 6, 2004
  3. sunrise
    Replies:
    3
    Views:
    339
    Ioannis Vranos
    Feb 12, 2005
  4. hank

    How to print an array of char backward.

    hank, May 30, 2008, in forum: C Programming
    Replies:
    37
    Views:
    849
    Tomás Ó hÉilidhe
    Jun 3, 2008
  5. Replies:
    2
    Views:
    449
Loading...

Share This Page