0

Is there any in-built function in perl to sort characters in string like sorted() in python?

For example,

$word = "honey";

i want output as "ehnoy".

3
  • That looks reversed, not sorted. Commented Aug 26, 2014 at 9:56
  • You mean you want to reverse it? Commented Aug 26, 2014 at 9:57
  • @izb:sorry. Sorting only. Commented Aug 26, 2014 at 10:00

2 Answers 2

3

You have to split string into list of chars, sort it, and join list of chars using empty string,

my $sorted = join "", sort split //, $word;

assigned ehnoy

Sign up to request clarification or add additional context in comments.

2 Comments

Thats i know that. I want to know about only in-built functions rather than making that as list of characters and then sort.
@user3440628 you can sort only lists; there is no built-in which could sort chars inside string.
2

You could embed mpapec's solution in a subroutine that makes it LOOK like Perl has it built in...

sub sorted
{
   join "", sort split //, shift;
}

my $str = "honey";

print sorted $str;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.