phparrayssortingstring-lengthcustom-sort

Sort a flat array with the shortest strings alternating the outermost elements and the longest string in the middle


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 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']

Solution

  • 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);