I'm trying to sort groups of three characters like LMH, MMH, HHL etc all containing only the characters L, M and H but I need them sorted in the order LMH. This is what I have but not sure how to compare them. The $val array doesn't work when there are duplicate characters. It's a string that is broken into an array by the function.
function sortit($str) {
$val = ['L' => 0, 'M' => 1, 'H' => 2];
$parts = str_split($str);
foreach ($parts as $value) {
$order[$val[$value]] = $value;
}
ksort($order);
return implode('',$parts);
}
Input:
MLH
HLL
MHM
LHM
MLH
LHM
MHL
Desired output:
LMH
LLH
MMH
LMH
LMH
LMH
LMH