3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function getSuitValue($card) { $result = 0; switch($card[1]) { case 'C' : $result *= 10; break; case 'H' : $result *= 100; break; case 'D' : $result *= 1000; break; default: $result *= 1; } return $result; } function getValue($card) { $result = 0; switch($card[0]) { case 'A' : $result += 1; break; case 'J' : $result += 11; break; case 'Q' : $result += 12; break; case 'K' : $result += 13; break; default: $result += (int)$card[0]; } return $result; } function isGreat($card1, $card2) { $val1 = getValue($card1); $val2 = getValue($card2); if ($val1 > $val2) { return true; } elseif ($val1 === $val2) { if (getSuitValue($card1) > getSuitValue($card2)) { return true; } } return false; } function mySort($arr) { for ($i = 1; $i < count($arr); $i++) { $x = $arr[$i]; for ($j = $i - 1; $j >= 0 && isGreat($arr[$j], $x); $j--) { $arr[$j + 1] = $arr[$j]; } // на оставшееся после сдвига место, ставим $a[$i] $arr[$j + 1] = $x; } return $arr; } function sort_cards($cards) { $array = explode(', ', $cards); $resultArr = mySort($array); return implode(', ', $resultArr); } echo sort_cards('1C, 5S, 4D'), PHP_EOL;
based on nsHhJ
Output for 4.3.10 - 4.4.9, 5.0.2 - 7.2.6
1C, 4D, 5S
Output for 4.3.0 - 4.3.9, 5.0.0 - 5.0.1
1C, 4D, 5S Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/WOIKf on line 60 PHP_EOL