Sort Options in a Select List with javascript/jQuery.. but not alphabetically

Viewed 35086

I am looking for a function that will take a list of options in a select list and sort them alphabetically but with a twist. All values with the text 'NA' should be pushed to the bottom of the list.

So given a list -

<select>
    <option value="1">Car</option>
    <option value="2">Bus</option>
    <option value="3">NA</option>
    <option value="4">Bike</option>
    <option value="5">Tractor</option>
    <option value="6">NA</option>
</select>

We should end up with -

<select>
    <option value="4">Bike</option>
    <option value="2">Bus</option>
    <option value="1">Car</option>
    <option value="5">Tractor</option>
    <option value="3">NA</option>
    <option value="6">NA</option>
</select>

The order of the NAs is not important.

And don't ask why I cannot just remove the NAs (or why there will be several options with the same texts but different underlying values, because I don't agree with it either.

4 Answers
Related