1

How can you sort the following word by the given rule?

Example data is saapas which I want to be either

aaapss

or in the array

s
a
a
p
a
s

and then somehow

a
a
a
p
s
s

The function arsort with sort_flags SORT_REQULAR and SORT_STRING did not work for me.

3 Answers 3

2
$string = 'saapas';
$string = str_split($string, 1);
sort($string);
echo implode('', $string);
Sign up to request clarification or add additional context in comments.

Comments

1

In your original post you said either a string or an array. eyze's solution works for the string but sort() will work for the array of values:

$array = array('s', 'a', 'a', 'p', 'a', 's');
sort($array);

//will output
Array
(
    [0] => a
    [1] => a
    [2] => a
    [3] => p
    [4] => s
    [5] => s
)

Comments

-1
$string = 'saapas';
echo implode(sort(str_split($string, 1)));

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.