0

Is it possible to change the order in the dropdown to replace an item?

enter image description here

I need to change it to alphabetic order. Any idea?

Thanks in advance!

  • Possible duplicate of [Sorting options elements alphabetically using jQuery](http://stackoverflow.com/questions/12073270/sorting-options-elements-alphabetically-using-jquery) – 4b0 Jan 30 '17 at 08:22

3 Answers3

0

Please find below snippet to sort selectlist

$("#mSelect").append($("#mSelect option").remove().sort(function(a, b) {
    var at = $(a).text(), bt = $(b).text();
    return (at > bt)?1:((at < bt)?-1:0);
}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="mSelect" >
    <option value="val1" > DEF </option>
    <option value="val4" > GRT </option>
    <option value="val2" > ABC </option>
    <option value="val3" > OPL </option>
    <option value="val5" > AWS </option>
    <option value="val9" > BTY </option>
</select>
Curiousdev
  • 5,668
  • 3
  • 24
  • 38
0

Small Correction: Added last line for clearing default option selection

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="mSelect" >
    <option value="val1" > DEF </option>
    <option value="val4" > GRT </option>
    <option value="val2" > ABC </option>
    <option value="val3" > OPL </option>
    <option value="val5" > AWS </option>
    <option value="val9" > BTY </option>
</select>

$("#mSelect").append($("#mSelect option").remove().sort(function(a, b) {
    var at = $(a).text(), bt = $(b).text();
    return (at > bt)?1:((at < bt)?-1:0);
}));

$("#mSelect").val("");
user7488658
  • 116
  • 2
0

At the moment there is no such option. A jQuery-Hacky method may be possible, but I wouldn't try it. Basically if you want this, you should create an issue on github, but you'll probably have to contribute the code :)

iJungleBoy
  • 5,325
  • 1
  • 9
  • 21