Question: I have an array of scripts

```
Array
(
    [0] => /js/ionslider/ion.rangeSlider.min.js
    [1] => /media/jquery/jQuery-2.1.4.min.js
    [2] => /media/bootstrap/js/bootstrap.min.js
    [3] => /widgets/wid_menu/js/menu.js
)

```

How can I sort this to get results:

```
Array
(
    [0] => /media/jquery/jQuery-2.1.4.min.js            
    [1] => /media/bootstrap/js/bootstrap.min.js
    [2] => /js/ionslider/ion.rangeSlider.min.js        
    [3] => /widgets/menu/js/menu.js
)

```

E.g put jQuery script and bootstrap at the top. 
P.S jQuery version file may be changed in the future.
Thanks!

Answer: 
There is not criterion in your sorting method. Maybe you can 'categorize' elements while adding them to array, or implement a dependence system.

Otherwise, you can set sort criteria in an array, i.e.:

```
$patterns = [ '/media/jquery/', '/media/bootstrap/', '/js/'  ];

```

that respect your desired sort order, then you can use a function to use with `usort`, like this one:

```
function scriptSort( $a, $b )
{
    $patterns = [ '/media/jquery/','/media/bootstrap/','/js/'  ];

    $keyA = $keyB = count( $patterns );
    foreach( $patterns as $key => $pattern )
    {
        if( strpos( $a, $pattern ) === 0 ) $keyA = $key;
        if( strpos( $b, $pattern ) === 0 ) $keyB = $key;
    }

    if( $keyA !== $keyB ) return $keyA - $keyB;
    else                  return $a > $b;
}

usort( $scripts, 'scriptSort' );

```

`**[eval.in demo](https://eval.in/537059)**`

In the custom function, first we set two variables (`$keyA` and `$keyB`) to total items of pattern array, then we search if a path pattern match passed value and - if it is - we set `$keyA/B` to corresponding array key. At the end, we return the key difference (at the start we set to total count, so if the key is not found, the `$keyA/B` value is > than found value), otherwise we sort alphabetically.
Usort is a good option. Another way to do this is to loop over the scripts, transferring them one at a time to a new array. If they contain one of the strings you are looking for, push them onto the beginning of the new array, and if they don't, push them onto the end.

```
foreach ($scripts as $script) {
    if (strpos($script, '/media/jquery') !== false 
        || strpos($script, '/media/bootstrap') !== false) {
        array_unshift($sorted_scripts, $script);             // add to beginning
    } else {
        $sorted_scripts[] = $script;                         // add to end
    }
}

```