I am showing ajax response in select options as below code. I want to sort the select options in alphabetical order of values.
Please provide a solution to sort below data on values basis. My data is like {123:"green",124:"blue"}
function ajax_post(params, append_to, appent_type) {
$.ajax({
type: "GET",
url: "<?= $this->getBaseUrl()?>" + "customcontroller/index/index/" + params,
dataType: "json",
success: function (data) {
if (append_to != 0) {
$(append_to).html('');
$(append_to).append('<option value="0">Select ' + appent_type + '</option>');
$.each(data, function (key, value) {
$(append_to).append('<option value="' + key + '">' + value + '</option>');
});
}
}
})
}
Please provide a solution. I have tried to sort response in controller but when ajax loads it, it shows unsorted data.
asort()
in your controller.