jQuery(document).ready(function($){
var mylist = $('#location');
var listitems = mylist.children('option').get();
listitems.sort(function(a, b) {
var compA = $(a).text().toUpperCase();
var compB = $(b).text().toUpperCase();
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
})
$.each(listitems, function(idx, itm) { mylist.append(itm); });
});
Всю документацию перерыл не помогло.
<select name="list" id="mylist" style="width:140px;">
<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>
$('#mylist').select2({
sortResults: function(data) {
/* Sort data using lowercase comparison */
return data.sort(function (a, b) {
a = a.text.toLowerCase();
b = b.text.toLowerCase();
if (a > b) {
return 1;
} else if (a < b) {
return -1;
}
return 0;
});
}
});
$('#e22').select2({
sortResults: function(results, container, query) {
if (query.term) {
// use the built in javascript sort function
return results.sort(function(a, b) {
if (a.text.length > b.text.length) {
return 1;
} else if (a.text.length < b.text.length) {
return -1;
} else {
return 0;
}
});
}
return results;
}
});