array sorting with blank spaces: do IE and Mozilla handle this differently?

J

John Bullock

Hello,

I am at wit's end with an array sorting problem. I have a
simple table-sorting function which must, at times, sort on columns that
include entries with nothing but a space (@nbsp;). I want all of the
spaces to be put in the first slots of the array. IE 6 does this. But
Firefox 0.9.1 doesn't, and I don't know why.
I have not been able to reproduce it in very simple form (which
is itself a puzzle). But example code is available at
http://cgi.stanford.edu/~bullock2/dirlist.pl?threat: at this page,
sorting on the "Size" column works as I want in IE, but not in Firefox.
I suspect that the problem lies with my code and not with the browsers.
For what it's worth, here is the sorting function:

function RowCompareNumbers(a, b) {
if (a.value==" " & b.value==" ") return 0;
else if (a.value==" ") return -1;
else if (b.value==" ") return 1;
else {
var aVal = parseInt(a.value);
var bVal = parseInt(b.value);
return (aVal - bVal);
}
}

(I know this can be made much more concise, but for the moment,
I'm not worried about it.)

There is also getInnerText() function which retrieves the text
from the table cells; it may be the culprit.

Many thanks,
--John
 
J

John Bullock

By the way, there is a long and useful thread from mid-February on
browser discrepancies in array sorting, but I couldn't see that it
helped for this particular problem.
 
J

John Bullock

I found an array sorting function that produced identical (and
desirable) behavior in both IE 6 and Firefox 0.9.1. The question
becomes: why do the two functions produce different behavior in FireFox
but not in IE?

The function that works for both browsers:

function RowCompareNumbersX(a, b) {
var aVal = parseInt(a.value);
var bVal = parseInt(b.value);
if (isNaN(aVal) && isNaN(bVal)) return 0;
else if (isNaN(parseInt(a.value))) return -1;
else if (isNaN(parseInt(b.value))) return 1;
else return (aVal - bVal);
}

The function that produces a different sort (from the one above) in
Firefox, but the same in IE:

function RowCompareNumbers(a, b) {
var aVal = parseInt(a.value);
var bVal = parseInt(b.value);
if (a.value==" " & b.value==" ") return 0;
else if (a.value==" ") return -1;
else if (b.value==" ") return 1;
else return (aVal - bVal);
}

Thanks again,
--John
 
L

Lasse Reichstein Nielsen

John Bullock said:
The question becomes: why do the two functions produce different
behavior in FireFox but not in IE?
The function that produces a different sort (from the one above) in
Firefox, but the same in IE:

function RowCompareNumbers(a, b) {
var aVal = parseInt(a.value);

This should be
var aVal = parseInt(a.value, 10);

The original version (without the base as argument to parseInt) will
read "012" as 10 in some browsers (read as octal because of initial
0).
var bVal = parseInt(b.value);
if (a.value==" " & b.value==" ") return 0;
^
^ that is bitwise and.

You compare the valus to strings of length 1 contatining only one
space. I'll habe to assume that you know that the value won't be the
empty string or two spaces or something else. However, my guess is
that this is the problem you are having.

Bitwise and turns both arguments into 32 bit integers and then
performs the bitwise and on these. In this case, it shouldn't matter,
since booleans turned into numbers become 0 and 1 (for false and true)
and bitwise and gives the same result as logical and.

/L
 
G

Grant Wagner

John said:
Hello,

I am at wit's end with an array sorting problem. I have a
simple table-sorting function which must, at times, sort on columns that
include entries with nothing but a space (@nbsp;). I want all of the
spaces to be put in the first slots of the array. IE 6 does this. But
Firefox 0.9.1 doesn't, and I don't know why.
I have not been able to reproduce it in very simple form (which
is itself a puzzle). But example code is available at
http://cgi.stanford.edu/~bullock2/dirlist.pl?threat: at this page,
sorting on the "Size" column works as I want in IE, but not in Firefox.
I suspect that the problem lies with my code and not with the browsers.
For what it's worth, here is the sorting function:

function RowCompareNumbers(a, b) {
if (a.value==" " & b.value==" ") return 0;

You're using a bitwise & here, not a logical one. Although it may be working
as expected most of the time, I'm sure it's not what you intended.
else if (a.value==" ") return -1;
else if (b.value==" ") return 1;
else {
var aVal = parseInt(a.value);
var bVal = parseInt(b.value);
return (aVal - bVal);
}
}

Your comparator is doing way more work then is necessary. You just need to
normalize any non-numeric data to numbers, in this case, zero (because you
want them first in the array). The way to achieve this is by converting each
value to a number, then changing anything that isNaN into 0. Here is a
sample that works in IE6SP1, Netscape 4.78, Opera 7.5.2 and Firefox 0.9.1:

// sample data
var myArray = [
{value:123},
{value:' '},
{value:234},
{value:' '},
{value:' '},
{value:345},
{value:' '}
];
// sort using custom comparator
myArray.sort(RowCompareNumbers);

// test output
for (var i = 0; i < myArray.length; i++) {
document.write('[' + myArray.value + ']<br>');
}

// comparator
function RowCompareNumbers(a, b) {
var aa = +a.value || 0;
var bb = +b.value || 0;
return (aa - bb);
}
(I know this can be made much more concise, but for the moment,
I'm not worried about it.)

Cleaning up the comparator to generate reliable, consistent sorted lists
with sample data is the first step in getting this working, so you should be
worried about it. You're doing parseInt() without a second parameter, so if
any of your data contains leading zeros, it'll be converted to hexadecimal,
which might contain letters, which would cause an error when you attempt to
"return (aVal - bVal);".
There is also getInnerText() function which retrieves the text
from the table cells; it may be the culprit.

Well then, what I'd suggest you do is populate an array with the data you
are getting from the table cells to see what that is exactly (by dumping it
to a <textarea> or alert()ing it for example.

Also, Mozilla doesn't support the "innerText" property, so I'm hoping your
"getInnerText()" function actually uses "innerHTML" (although it should be
using nodeValue). Even if it does use "innerHTML", the representation of
"innerHTML" differs from browser to browser (see <url:
http://jibbering.com/faq/faq_notes/alt_dynwrite.html#innHTest />) so what
IE is retrieving from the table cell might be completely different from what
Mozilla is retrieving.

Check the data you are trying to sort before checking anything else
(although you can use my comparator). Garbage in, garbage out as they say.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top