Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | Form 4 | Q487 [ previous next ]

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

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/index.htm, and then replace the existing options with the contents of the sorted user defined object array.


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.