Check this option . Although I haven't tried it, it looks like this might do the sorting job.
On the verification of existence. Basically you can check:
<script type="text/javascript"> $(document).ready(function() { if($('#mySelect optgroup[label=New group]').html() == null){ $('#mySelect').append('<optgroup label="New group"></optgroup>'); $('#mySelect optgroup[label=New group]').append('<option value="1">New element</option>'); } else { $('#mySelect optgroup[label=New group]').append('<option value="1">New element</option>'); } }); </script>
The script checks if there is an opt group with a New Lab group, if there is no such group, add a new group, and then add an option to this group. Part of else, just add the parameter to the existing new group.
Then you should apply sorting with the plugin.
source share