I would like to sort alphabetically (case insensitive), the tags choosen from a select2. I have used the sorter function to sort the tags to be selected but once selected they are not displayed alphabetically.
How to do this ? (I work with last 4.1.0-rc.0 select2 release).
$('#example1').select2({
tags: true,
multiple: true,
allowClear: true,
sorter: function(data) {
return data.sort(function (a, b) {
var a1 = a.text.toLowerCase();
var b1 = b.text.toLowerCase();
if (a1 > b1) {
return 1;
}
if (a1 < b1) {
return -1;
}
return 0;
});
}
});
and
<select name="list" id="example1" style="width:500px;">
<option>United States</option>
<option>Austria</option>
<option>Alabama</option>
<option>Jamaica</option>
<option>Taiwan</option>
<option>canada</option>
<option>palau</option>
<option>Wyoming</option>
</select>
Have posted the question as well on
See the last jsfiddle with a proposed solution but that does not work at initialization.
Do not understand why.
https://jsfiddle.net/PBrockmann/x42sqg76/
Just add ‘BBBB’, then the order will be correct.
Otherwise ‘AAAA’ is at the end !