Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q487 Is there some intelligent way to sort document.formname.selectname.options array by text/value so that selected option (or options) remains selected?

You are here: irt.org | FAQ | JavaScript | Form | 4 | Q487 [ previous next ]

There are intelligent ways to sort, including the sort() method for an array introduced in Navigator 3.

But if you are trying to sort something that has two attributes, the value and text, along with an indication as to whether the option is selected, then you need to be able to sort an array of options. The inbuilt sort() method, however, sorts an array and not an object array, so you need to create your own sort routine.

The way I would tackle it is to copy the value, text and selected properties for each option into a user defined object array. Then sort the object array using the techniques described in the article Arrays, Object Arrays and Sorting at http://www.irt.org/articles/js054, and then replace the existing options with the contents of the sorted user defined object array.

©2018 Martin Webb