I have jQuery but I’m not sure if it has any built-in sorting helpers. I could make a 2d array of each item’s text, value, and selected properties, but I don’t think that javascript’s built in Array.sort() would work correctly.
I have jQuery but I’m not sure if it has any built-in sorting helpers.
Share
Extract options into a temporary array, sort, then rebuild the list:
Mozilla’s sort documentation (specifically the compareFunction) and Wikipedia’s Sorting Algorithm page are relevant.
If you want to make the sort case insensitive, replace
textwithtext.toLowerCase()The sort function shown above illustrates how to sort. Sorting non-english languages accurately can be complex (see the unicode collation algorithm). Using localeCompare in the sort function is a good solution, eg: