Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Mean array sorting bug

3 views
Skip to first unread message

Branimir Petrovic

unread,
Apr 30, 2002, 1:46:34 AM4/30/02
to
This one ate my whole evening:

Array sort method using custom function as parameter will fail with
error # 800A1389 inside custom sort function. Fails - but not always.
Whether will it fail, depents on array size!? Remove any item from
below array, and code works (as it always should); run as is and see
it miserably failing. Arrrgh!!!!

Are the chances of seeing this bug ever fixed zero or mere non existant?
Any case too late for me and this current project :(

My system: WSH5.6, Win2K SP2 + all critical updates.

Branimir


/*////////////////////////////////////////////////////////////////////
FileName: SortBug.js
////////////////////////////////////////////////////////////////////*/

var oAry = new Array("Microsoft Windows 2000 Professional",
"ActivePython-2.2 --Version: 2.2.221",
"ActiveState Komodo 1.1.2 Build 23917 --Version: 1.1.23917",
"Ad-aware 5.62",
"AutoIt v2.63 --Version: 2.63",
"Common Setup Files (3590.2) --Version: 5.1.3590.2",
"EditPlus 2",
"Exchange Server SDK (3590.2) --Version: 5.1.3590.2",
"HyperTerminal Private Edition v6.3",
"JScript V5.5 Documentation",
"Java 2 Runtime Environment Standard Edition v1.3.1",
"Java 2 SDK Standard Edition v1.3.1",
"Lavasoft RefUpdate 1.3 --Version: 1.1",
"LiveUpdate 1.7 (Symantec Corporation)",
"Vim 6.0 (self-installing)",
"WebFldrs --Version: 9.00.3501",
"WinZip",
"WinZip Command Line Support Add-On",
"Windows 2000 Application Compatibility Update",
"Windows 2000 Security Rollup Package (See Q311401 for more information)",
"Windows Installer SDK (Version 2.0) (3590.2) --Version: 5.1.3590.2",
"Windows Management Instrumentation (WMI) SDK 2001 (3590.2) --Version: 5.1.3590.2",
"Windows Media Player 7.1",
"Windows Script Host 2.0 Documentation",
"Windows Script V5.6 Documentation",
"w3JMail4");

oAry.sort(sortAsc);
WScript.Echo(oAry);

function sortAsc(a, b) {
// Implements case insensitive string sort (keeping original casing intact)
a = a.toUpperCase();
b = b.toUpperCase();

if (a>b) return 1; // <<< Microsoft JScript runtime error: Number expected <<<

// "a" turns out to be an object of "unknown" type,
// and as such can not be used for meaningfull comparison.
// Shorten initial length of the array (drop the item or two),
// and all of sudden all works like a charm?!

if (a<b) return -1;
return 0;
}


Michael Harris (MVP)

unread,
Apr 30, 2002, 4:59:56 PM4/30/02
to
> This one ate my whole evening:
>
> Array sort method using custom function as parameter will fail with
> error # 800A1389 inside custom sort function. Fails - but not always.
> Whether will it fail, depents on array size!? Remove any item from
> below array, and code works (as it always should); run as is and see
> it miserably failing. Arrrgh!!!!
>
> Are the chances of seeing this bug ever fixed zero or mere non existant?
> Any case too late for me and this current project :(
>
> My system: WSH5.6, Win2K SP2 + all critical updates.
>
> Branimir
>


And it gets even stranger ;-)...

I added: WScript.echo(oAry.length);

right before the array is sorted, and the error disappeared, but it reappeared if I added another element to the array, then it disappeared if I added another element to the array, then it reappeared...

With the echo of the length, failures occurred when the array had an even number of elements.

Without the echo, failures occurred when the array had an odd number of elements.

When I started *removing* elements, it became "stable" at 20 or less. When adding elements the failure pattern just kept repeating the more rows I added (I quit in the 150's).

Test was on Win2K sp2 w/ WSH 5.6.

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US
--


Michael Harris (MVP)

unread,
Apr 30, 2002, 5:53:52 PM4/30/02
to
An email reply I got from Mike Whalen...

<quote>

Hmmm... looks like the bug has something to do with the following lines:

a = a.toUpperCase();
b = b.toUpperCase();

For some reason, toUpperCase occasionally is not returning a string object, or the value is getting stored improperly, or something else that I haven't thought of.

All is not lost, though - there's an easy workaround.... do:

function sortAsc(ta, tb) {
a = ta.toUpperCase();
b = tb.toUpperCase();
...

and the function appears to work regardless of the number of lines or the WScript.Echo line.

-- Mike

</quote>

Branimir Petrovic

unread,
Apr 30, 2002, 7:19:08 PM4/30/02
to
You da man Michael!

Didn't know what to do with it and disliked the idea of having to turn to
ADO and recordset for sort, but this workaround does not hurt and does the job.
Now I'll be able to finish what I started. Thanks a ton both to you for taking
initiative and to Mike for pinpointing magic workaround!

Branimir

"Michael Harris (MVP)" <mik...@mvps.org> wrote in message news:eCPkFGJ8BHA.1920@tkmsftngp07...

Hans Gommers

unread,
May 1, 2002, 3:58:08 AM5/1/02
to
Nice one michael.

Seeing your solution makes me wonder. Is the string passed to the
sortfunction as reference or as value? If it's converted to a string object
and passed as reference you'd be messing with the original array. That would
explain why the fix works?

I guess not as Branamir probably would have seen changes in the result.

Hans

"Branimir Petrovic" <The...@NOSPAM.net> schreef in bericht
news:uaVjw1J8BHA.1364@tkmsftngp02...

Michael Harris (MVP)

unread,
May 1, 2002, 5:40:14 PM5/1/02
to
> Nice one michael.
>

You should be addressing Mike Whalen, not me - I'm just the messenger here <g>!


> Seeing your solution makes me wonder. Is the string passed to the
> sortfunction as reference or as value? If it's converted to a string object
> and passed as reference you'd be messing with the original array. That would
> explain why the fix works?
>

JScript passes only by value. Even in JScript.Net, pass by ref is supported only for external method calls.

Thor Larholm

unread,
May 6, 2002, 8:14:30 AM5/6/02
to
"Michael Harris (MVP)" <mik...@mvps.org> wrote in message
news:eCPkFGJ8BHA.1920@tkmsftngp07...
> For some reason, toUpperCase occasionally is not
> returning a string object, or the value is getting stored
> improperly, or something else that I haven't thought of.

My guess is that the sort method messes this up by interchanging between native
JS datatypes and internal C/C++ datatypes/pointers. When I fire up the VS.Net
debugger, a is of "User-defined Type" (no longer a JS datatype) whereas b is
still of "String" type.


--
Thor Larholm
<URL: http://www.jibbering.com/faq/> FAQ for comp.lang.javascript
<URL: http://jscript.dk/unpatched/> Unpatched IE vulnerabilities


Branimir Petrovic

unread,
May 6, 2002, 9:19:38 PM5/6/02
to

"Thor Larholm" <th...@jubii.dk> wrote in message news:#EMneeP9BHA.2356@tkmsftngp07...

> "Michael Harris (MVP)" <mik...@mvps.org> wrote in message
> news:eCPkFGJ8BHA.1920@tkmsftngp07...
> > For some reason, toUpperCase occasionally is not
> > returning a string object, or the value is getting stored
> > improperly, or something else that I haven't thought of.
>
> My guess is that the sort method messes this up by interchanging between native
> JS datatypes and internal C/C++ datatypes/pointers.

Whatever the cause, it would be nice to see it fixed eventually if not soon.

Aparently not very many people fiddle with custom sort method functions (could
it be due to N (where N==0) examples on the subject in the documentation?),
otherwise I would expect this particular bug to be discovered/fixed some
(long) time ago.

Branimir

0 new messages