As an academic/contrived task, I'd like to sort an indexed, 1-dimensional array so that the longest string is in the middle and its neighbouring elements should be the 2nd and 3rd longest and so on.
How can I alternate the position of the shortest strings working from the front and back of the array, and finish with the longest string(s) in the middle?
Sample input:
$array = ["Hello", "World", "this", "is", "a", "test"];
Desired result:
["a", "test", "Hello", "World", "this", "is"]
Explanation:
a
is shortest and must be positioned firstis
is 2nd shortest, so it takes last positiontest
and this
are tied for 3rd shortest, alphabetize to break the tie which determines that test
comes after a
and this
comes before is
Hello
and World
are tied for the longest strings, so alphabetize to break the tie and position Hello
after test
and World
before this
A secondary example with an odd number of elements:
$array = ['hundreds', 'human', 'AI', 'replace', 'may', 'developers', 'soon'];
Desired result:
['AI', 'soon', 'replace', 'developers', 'hundreds', 'human', 'may']
Here is one way to do it (there's most likely a better solution):
function sort_length($str1, $str2)
{
if(strlen($str1) == strlen($str2))
{
return 0;
}
return strlen($str1) > strlen($str2) ? -1 : 1;
}
$words = array("Hello", "World", "this", "is", "a", "test");
usort($words, 'sort_length');
$new_list = array();
$boolean = true;
foreach($words as $word)
{
if($boolean)
{
array_push($new_list, $word);
}
else
{
array_unshift($new_list, $word);
}
$boolean = !$boolean;
}
//print_r($new_list);