How I can change order after selected option


I would like to ask if it is possible to change the order of the items after selection. I need to put the currently selected items at the top of the list.

Thanks for answer.

It is possible. There is a sorter configuration option, which is not otherwise documented on the site, that controls the order the items appear in the Select2’s dropdown list. The callback function you provide for this configuration option receives an array of objects representing the Select2’s items and should return another array containing those objects in the order you wish for them to appear in the dropdown. Within the array you receive, the data objects that represent the selected items will include a selected: true property. You can write an array sorting function that will sort the options first by selected status and then by their text value, and use that function to sort the incoming array and then return the sorted array.