Last Updated: September 09, 2019
·
9.038K
· albertovelandia

Order Select Options Alphabetically

IF you ever have to organize a Select alphabetically you can use this Little Snippet I assume you use jQuery but you can always apply the same logic to make it just Javascript

See it live here: https://tinker.io/37028/3

function orderSelect($element) {
    var options   = $element.find('option'),
        n_options = options.length,
        temp = [],
        parts,
        i;

    for(i = n_options; i --;) {
        temp[i] = options[i].text + "," + options[i].value ; 
    }

    temp.sort();

    for(i = n_options; i --;) {
        parts = temp[i].split(',');

        options[i].text  = parts[0];
        options[i].value = parts[1]; 
    }
}