Fastest array sorting: VBScript or Javascript?

Results 1 to 2 of 2

Thread: Fastest array sorting: VBScript or Javascript?

  1. #1
    Join Date
    Dec 1969

    Default Fastest array sorting: VBScript or Javascript?

    Below is the current (Javascript) way we are sorting two SELECT controls after we move one value (a name) from one SELECT to the other. <BR>It takes a while to update the controls and I&#039;m wondering if I read the values into a VBScript recordset and used the recordset&#039;s sort method, if that would be faster...<BR><BR>There are potentially 481 values in the select box to sort.<BR><BR>Any ideas?<BR><BR>We send the control to this function:<BR>function sortSelect(obj) {<BR> var o = new Array();<BR> for (var i=0; i&#060;obj.options.length; i++) {<BR> o[o.length] = new Option( obj.options[i].text, obj.options [i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;<BR> }<BR> <BR> o = o.sort( <BR> function(a,b) { <BR> if ((a.text+"") &#060; (b.text+"")) { return -1; }<BR> if ((a.text+"") &#062; (b.text+"")) { return 1; }<BR> return 0;<BR> } <BR> );<BR><BR> for (var i=0; i&#060;o.length; i++) {<BR> obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);<BR> }<BR>}<BR><BR>Thanks in advance,<BR><BR>Barry Watson<BR>

  2. #2
    Join Date
    Dec 1969

    Default VBS doesn't even have sort...

    ...built in! If you look in the ASPFAQs, you&#039;ll see that the recommended way to sort a VBS array is to convert it to JS, sort it there, and then rebuild the array back in VBS!<BR><BR>I&#039;m truly surprised that you can see the time involved in doing this.<BR><BR>Have you ever put in a timer function to see where the time is being spent?<BR><BR>I&#039;d almost bet that it is the construction of all those Option objects that&#039;s a killer.<BR><BR>You might get a bit better perf by using an array-of-arrays instead of the array-of-Options. And maybe prebuild the outer array to the right size?<BR><BR>function sortSelect(obj) <BR>{<BR> var o = new Array(obj.options.length);<BR> for (var i=0; i&#060;obj.options.length; i++) <BR> {<BR> o[i] = new Array( obj.options[i].text, obj.options [i].value, obj.options[i].defaultSelected, obj.options[i].selected);<BR> }<BR><BR> o = o.sort( <BR> function(a,b) { <BR> if ( a[0] &#060; b[0] ) return -1;<BR> if ( a[0] &#062; b[0] ) return 1; <BR> return 0;<BR> } <BR> );<BR><BR> for (var i=0; i&#060;o.length; i++) {<BR> obj.options[i] = new Option(o[i][0], o[i][1], o[i][2], o[i][3]);<BR> }<BR>}<BR><BR>And do you *really* need to copy the .defaultSelected?<BR><BR>(For that matter, do you really need to copy the .selected? If you just moved an item into this &#060;SELECT&#062;, then presumably nothing is selected, anyway.)<BR><BR>I dunno...500 items being sorted this way should be nearly unnoticeable time. You&#039;ve got me.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts