tags:

views:

165

answers:

3

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.

A: 
$string = 'saapas';
echo implode(sort(str_split($string, 1)));
powtac
sort() returns by reference, I think this doesn't work.
Alix Axel
yea, `sort()` returns a boolean: http://www.php.net/manual/en/function.sort.php
o.k.w
+2  A: 
$string = 'saapas';
$string = str_split($string, 1);
sort($string);
echo implode('', $string);
Alix Axel
+1  A: 

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
)
jeerose