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: 
You can use usort($arr, "cmp"); and use a custom sorting function. Then in that sorting function you can define a preg\_match that can fit your needs with a regular expression that validates for a string that contains "jquery" with any version and "bootstrap".

[Sorting by Regex Groups in PHP](https://stackoverflow.com/questions/8447281/sorting-by-regex-groups-in-php)
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
    }
}

```