按字母顺序排序选择

I am wondering how to sort select options alphabetically. I was reading on another page where you can jQuery and I completely get the hang of it, I'm just confused a tad bit how I'd use it on the code I use for my selecting.

Here's my selection code:

<select class="form-control" id="rareImage" name="rareImage">
<?php
    if($open = opendir(ROOT .'/resources/images/small_furni')) {
        while(false != ($file = readdir($open))) {
            if($file == '.' || $file == '..') {
                continue;
            }
            echo '<option value="'. $file .'" '. ((isset($rareimage) && $rareimage == $file) ? 'selected' : '') .'>'. $file .'</option>';
        }
    }
?>
</select>

The other page suggested giving it a option value and then using jQuery to sort it. But like I said, I'm kind of confused how I'd go about using it on this code. Basically, the above code outputs a list of images to select from, but they're not in alphabetical order.

Reference I used: http://jsfiddle.net/3YjNR/2/

What i did, is that i put all the values i got from the while loop, into an array. I then sorted the array, and looped through it with a foreach... I think there is a better/faster way to do it, but this is what i got so far.

<select class="form-control" id="rareImage" name="rareImage">
<?php
$arr=array();
if($open = opendir(ROOT .'/resources/images/small_furni')) {
    while(false != ($file = readdir($open))) {
        if($file == '.' || $file == '..') {
            continue;
        }
        $files[]=$file;
    }
}
sort($files);
foreach($files as $file) echo '<option value="'. $file .'" '. ((isset($rareimage) && $rareimage == $file) ? 'selected' : '') .'>'. $file .'</option>';
?>
</select>