Sorting Array of String Backward

N

nntp

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.
 
P

Paul Lalli

nntp said:
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
 
J

Jürgen Exner

nntp said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top