473,466 Members | 1,301 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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


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
Jul 23 '05 #1
4 2515

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.
"John Bullock" <jo**********@stanford.edu> wrote in message
news:cc**********@news.Stanford.EDU...

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


Jul 23 '05 #2

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

"John Bullock" <jo**********@stanford.edu> wrote in message
news:cc**********@news.Stanford.EDU...

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


Jul 23 '05 #3
"John Bullock" <jo**********@stanford.edu> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4
John Bullock wrote:
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:'&nbsp;'},
{value:234},
{value:' '},
{value:'&nbsp;'},
{value:345},
{value:' '}
];
// sort using custom comparator
myArray.sort(RowCompareNumbers);

// test output
for (var i = 0; i < myArray.length; i++) {
document.write('[' + myArray[i].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/a....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.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jean-Marc Molina | last post by:
Hello, I can't find a way to execute a Windows application, whose directory path contains blank spaces, from a PHP script. I also wonder if the problem happens under Linux and other OS. ...
11
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? ...
3
by: gambler | last post by:
let's say you have: var games = new Array(); games = new GAME(gameNum, rotNum1, rotNum2, ... ); ( so a sparsley populate array which enables me to locate a game usin the game number...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
3
by: Chris Sharman | last post by:
Are spaces allowed in names ? Eg <input name="my field" type="text" value="my data"> The html4 dtd seems to say this is cdata, which allows embedded single spaces, but say agents may trim...
3
by: yehaimanish | last post by:
I am developing an application by which to parse the content from the access_log and insert it into the database. Since each row is an different entry, I am using file() to get the contents into an...
30
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
3
by: gemguy | last post by:
hi, I have a issue with an array values... Im having values repeatedly stored in an array and i want to delete those repeated values using javascript. * I tried it by storing in an array and...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.