the description

This program takes a string as input, and rearanges it to sort the words alphabetically and the numbers numerically. Each position in the resulting string with be the same data type as the input string. In other words, if the input string begins with a word, the resulting string will begin with a word. If the second position was a number, the second position in the result witll also be a number.

This project came about in a similar way as the anagram program I wrote a while back. A friend and I were talking about a problem given to somebody we know as a part of a job interview. Apparently software engineers for hedge funds need to know how to sort strings in this fashion? Anyway, as were talking about it, I was already formulating a solution in my head. I just figured I'd formalize my thoughts and actually write the program.

The original problem for the job interview was supposed to be done in C++ or Java. I have done it in PHP, but the core concepts I believe would be the same in any other programming language.

On a side note, I wrote this program entirely on an iPad. This is the first time I've ever used an iPad for development, and with the help of a bluetooth keyboard and Koder (an app I'm thoroughly impressed with so far) the process was no less seamless than if I was on my iMac or PC. The programming part took me maybe an hour on Christmas afternoon in Ridgway, Colorado while waiting for dinner.

the implementation

the code


$input = htmlentities($_POST['the_input']);
$input_array = explode(' ', $input);
$count = count($input_array);

if($count == 0) {
	echo "<p>C'mon, try actually entering something this time.</p>";
} elseif($count > 1000 || strlen($input) > 10000) {
	echo "<p>Let's try something a little shorter this time, whadda'ya say?</p>";
} else {
	$int_array = array();
	$int_indeces = array();
	$str_array = array();
	$str_indeces = array();
	for($i=0; $i<$count; $i++) {
		if(is_numeric($input_array[$i])) {
			// It's a number
			$int_array[] = $input_array[$i];
			$int_indeces[] = $i;
		} else {
			// It's a string
			$str_array[] = $input_array[$i];
			$str_indeces[] = $i;
	$ouput_array = array();
	for($i=0; $i<count($int_array); $i++) {
		$output_array[$int_indeces[$i]] = $int_array[$i];
	for($i=0; $i<count($str_array); $i++) {
		$output_array[$str_indeces[$i]] = $str_array[$i];
	echo '<p><strong>input:</strong> ' . $input . '</p>';
	echo '<p><strong>output:</strong> ';
	for($i=0; $i<$count; $i++) {
		echo $output_array[$i];
		if($i != $count + 1) {
			echo ' ';
	echo '</p>';

} // ends if $count != 0