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.
Maybe it's better to call it 'reordering' not 'sorting'

However, there are various ways you can do it.

I just made up a simple one:

```
$current = array(
    '/js/ionslider/ion.rangeSlider.min.js',
    '/media/jquery/jQuery-2.1.4.min.js',
    '/media/bootstrap/js/bootstrap.min.js',
    '/widgets/wid_menu/js/menu.js',
);

$priorities = array(
    'jquery',
    'bootstrap',
    'menu',
    );

function reorder($array, $priors){
    $result = array();
    foreach ($priors as $p) {
        $result = array_unique (array_merge ($result , preg_grep ('/'.$p.'/i', $array)));
    }

    return array_unique (array_merge ($result , $array));
}

echo '<pre>'.print_r(reorder($current,$priorities),true).'</pre>';

```